浙江大学PAT_甲级_1052. Linked List Sorting (25)

本文介绍了一种使用C++实现的链表排序算法。通过两次遍历链表,第一次收集所有节点信息,第二次按顺序重新组织节点,最终实现链表节点按键值升序排列。

题目链接:点击打开链接

A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive N (< 105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Key Next

where Address is the address of the node in memory, Key is an integer in [-105, 105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.

Output Specification:

For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.

Sample Input:
5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345
Sample Output:
5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1

首先用一个列表接收输入的节点信息,然后将第一个列表的节点按照前、后节点顺序放进第二个列表,对第二个列表排序,并输出。

我的C++代码:

#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
	int address, key, next;
};
bool compare(node a, node b)//自定义排序方法
{
	return a.key < b.key;
}
node list1[100000], list2[100000];
int main()
{
	int i = 0,index=0;//index是另一个列表的下标
	int n, first_address;//n节点个数,first_addresss第一个节点的位置
	int temp_address, temp_key, temp_next;
	node temp;
	scanf("%d %d", &n, &first_address);
	for (i = 0; i < n; i++)
	{
		scanf("%d %d %d", &temp_address, &temp_key, &temp_next);//输入节点地址 键 下一点地址
		list1[temp_address].address = temp_address;
		list1[temp_address].key = temp_key;
		list1[temp_address].next = temp_next;
	}
	if (first_address == -1)//一个节点都没有
	{
		printf("0 -1\n");
		return 0;//直接退出程序
	}
	while (first_address != -1)//从头节点位置开始,按顺序将节点串联起来,放进第二个列表
	{
		list2[index].address = list1[first_address].address;
		list2[index].key = list1[first_address].key;
		list2[index].next = list1[first_address].next;
		first_address = list1[first_address].next;
		index++;//第二个列表长度增加1
	}
	sort(list2, list2 +index , compare);//对第二个列表排序
	printf("%d %05d\n", index, list2[0].address);//输出第二个列表节点数,和头节点地址
	for (i = 0; i <index-1; i++)
	{
		printf("%05d %d %05d\n", list2[i].address, list2[i].key, list2[i + 1].address);
		//list2[i + 1].address是下一个节点位置
	}
	printf("%05d %d -1", list2[index- 1].address, list2[index - 1].key);//最后一个节点下一个位置是-1
	//system("pause");
	return 0;
}


#include "linked_list.h" linked_list_ptr LINKED_LIST_CREATE () { linked_list_ptr linked_list_ptr_H = NULL; if (NULL == (linked_list_ptr_H = (linked_list_ptr)malloc (sizeof (linked_list_node)))) { return linked_list_ptr_H; } memset (&(linked_list_ptr_H->data), 'H', sizeof (linked_list_ptr_H->data)); linked_list_ptr_H->next = NULL; return linked_list_ptr_H; } int LINKED_LIST_FREE (linked_list_ptr linked_list_ptr_H) { if (NULL == linked_list_ptr_H) { return -1; } int num = -1; linked_list_ptr linked_list_ptr_node = linked_list_ptr_H; while (NULL != (linked_list_ptr_node = linked_list_ptr_node->next)) { free (linked_list_ptr_H); num ++; linked_list_ptr_H = linked_list_ptr_node; } free (linked_list_ptr_H); num ++; return num; } int LINKED_LIST_COUNT (linked_list_ptr linked_list_ptr_H) { int num = -1; while (NULL != linked_list_ptr_H) { linked_list_ptr_H = linked_list_ptr_H->next; num ++; } return num; } int LINKED_LIST_INSERT (linked_list_ptr linked_list_ptr_H, data_t data, int pos) { if (NULL == linked_list_ptr_H) { return -1; } int num = 0; if (-1 == pos) { while (NULL != linked_list_ptr_H->next) { linked_list_ptr_H = linked_list_ptr_H->next; num ++; } if (NULL == (linked_list_ptr_H->next = (linked_list_ptr)malloc (sizeof (linked_list_node)))) { return -2; } linked_list_ptr_H = linked_list_ptr_H->next; linked_list_ptr_H->data = data; linked_list_ptr_H->next = NULL; } else { while (NULL != linked_list_ptr_H->next && 0 != pos --) { linked_list_ptr_H = linked_list_ptr_H->next; num ++; } if (NULL == linked_list_ptr_H->next) { if (NULL == (linked_list_ptr_H->next = (linked_list_ptr)malloc (sizeof (linked_list_node)))) { return -2; } linked_list_ptr_H = linked_list_ptr_H->next; linked_list_ptr_H->data = data; linked_list_ptr_H->next = NULL; } else { linked_list_ptr linked_list_ptr_node = linked_list_ptr_H->next; if (NULL == (linked_list_ptr_H->next = (linked_list_ptr)malloc (sizeof (linked_list_node)))) { return -2; } linked_list_ptr_H = linked_list_ptr_H->next; linked_list_ptr_H->data = data; linked_list_ptr_H->next = linked_list_ptr_node; } } return num; } int LINKED_LIST_DELETE (linked_list_ptr linked_list_ptr_H, int pos) { if (NULL == linked_list_ptr_H) { return -1; } if (NULL == linked_list_ptr_H->next) { return -2; } int num = 0; if (-1 == pos) { while (NULL != linked_list_ptr_H->next->next) { linked_list_ptr_H = linked_list_ptr_H->next; num ++; } free (linked_list_ptr_H->next); linked_list_ptr_H->next = NULL; } else { while (NULL != linked_list_ptr_H->next->next && 0 != pos --) { linked_list_ptr_H = linked_list_ptr_H->next; num ++; } if (NULL == linked_list_ptr_H->next->next) { free (linked_list_ptr_H->next); linked_list_ptr_H->next = NULL; } else { linked_list_ptr linked_list_ptr_node = linked_list_ptr_H->next->next; free (linked_list_ptr_H->next); linked_list_ptr_H->next = linked_list_ptr_node; } } return num; } int LINKED_LIST_PURGE (linked_list_ptr linked_list_ptr_H) { if (NULL == linked_list_ptr_H) { return -1; } if (NULL == linked_list_ptr_H->next) { return 0; } if (NULL == linked_list_ptr_H->next->next) { return 1; } int num = 2; linked_list_ptr linked_list_ptr_node1 = NULL, linked_list_ptr_node2 = NULL; while (NULL != (linked_list_ptr_H = linked_list_ptr_H->next)->next->next) { linked_list_ptr_node1 = linked_list_ptr_H; while (NULL != linked_list_ptr_node1->next->next) { if (linked_list_ptr_H->data == linked_list_ptr_node1->next->data) { linked_list_ptr_node2 = linked_list_ptr_node1->next->next; free (linked_list_ptr_node1->next); linked_list_ptr_node1->next = linked_list_ptr_node2; continue; } linked_list_ptr_node1 = linked_list_ptr_node1->next; } if (linked_list_ptr_H->data == linked_list_ptr_node1->next->data) { free (linked_list_ptr_node1->next); linked_list_ptr_node1->next =NULL; } num ++; } return num; } linked_list_ptr LINKED_LIST_LOCATE_NUM2ADD (linked_list_ptr linked_list_ptr_H, int pos) { if (NULL == linked_list_ptr_H) { return NULL; } if (NULL == linked_list_ptr_H->next) { return NULL; } if (-1 == pos) { while (NULL != (linked_list_ptr_H = linked_list_ptr_H->next)->next); } else { while (NULL != (linked_list_ptr_H = linked_list_ptr_H->next) && 0 != pos --); } return linked_list_ptr_H; } int LINKED_LIST_LOCATE_ADD2NUM (linked_list_ptr linked_list_ptr_H, linked_list_ptr linked_list_ptr_node) { if (NULL == linked_list_ptr_H) { return -1; } if (NULL == linked_list_ptr_node) { return -2; } if (linked_list_ptr_H == linked_list_ptr_node) { return -3; } if (NULL == linked_list_ptr_H->next) { return -4; } int num = 0; while (NULL != (linked_list_ptr_H = linked_list_ptr_H->next) && linked_list_ptr_node != linked_list_ptr_H) { num ++; } if (linked_list_ptr_H != linked_list_ptr_node) { return -5; } return num; } int LINKED_LIST_REVERSE (linked_list_ptr linked_list_ptr_H) { if (NULL == linked_list_ptr_H) { return -1; } if (NULL == linked_list_ptr_H->next) { return 0; } if (NULL == linked_list_ptr_H->next->next) { return 1; } int num = 2; linked_list_ptr linked_list_ptr_node1 = linked_list_ptr_H->next, linked_list_ptr_node2 = linked_list_ptr_H->next->next, linked_list_ptr_node3 = linked_list_ptr_H->next->next->next; linked_list_ptr_node1->next = NULL; while (NULL != linked_list_ptr_node3) { linked_list_ptr_node2->next = linked_list_ptr_node1; linked_list_ptr_node1 = linked_list_ptr_node2; linked_list_ptr_node2 = linked_list_ptr_node3; linked_list_ptr_node3 = linked_list_ptr_node3->next; num ++; } linked_list_ptr_node2->next = linked_list_ptr_node1; linked_list_ptr_H->next = linked_list_ptr_node2; return num; } int LINKED_LIST_BUBBLE_SORT_MIN (linked_list_ptr linked_list_ptr_H) { int num = -1; linked_list_ptr linked_list_ptr_node = linked_list_ptr_H; while (NULL != linked_list_ptr_node) { linked_list_ptr_node = linked_list_ptr_node->next; num ++; } if (2 > num) { return num; } linked_list_ptr linked_list_ptr_node1 = linked_list_ptr_H->next, linked_list_ptr_node2 = linked_list_ptr_H->next->next; for (int i = 0; i < num - 1; i ++) { for (int j = 0; j < num - 1 - i; j ++) { if (linked_list_ptr_node1->data > linked_list_ptr_node2->data) { linked_list_ptr_node1->data ^= linked_list_ptr_node2->data; linked_list_ptr_node2->data ^= linked_list_ptr_node1->data; linked_list_ptr_node1->data ^= linked_list_ptr_node2->data; } linked_list_ptr_node1 = linked_list_ptr_node2; linked_list_ptr_node2 = linked_list_ptr_node2->next; } linked_list_ptr_node1 = linked_list_ptr_H->next; linked_list_ptr_node2 = linked_list_ptr_H->next->next; } return num; } int LINKED_LIST_BUBBLE_SORT_MAX (linked_list_ptr linked_list_ptr_H) { int num = -1; linked_list_ptr linked_list_ptr_node = linked_list_ptr_H; while (NULL != linked_list_ptr_node) { linked_list_ptr_node = linked_list_ptr_node->next; num ++; } if (2 > num) { return num; } linked_list_ptr linked_list_ptr_node1 = linked_list_ptr_H->next, linked_list_ptr_node2 = linked_list_ptr_H->next->next; for (int i = 0; i < num - 1; i ++) { for (int j = 0; j < num - 1 - i; j ++) { if (linked_list_ptr_node1->data < linked_list_ptr_node2->data) { linked_list_ptr_node1->data ^= linked_list_ptr_node2->data; linked_list_ptr_node2->data ^= linked_list_ptr_node1->data; linked_list_ptr_node1->data ^= linked_list_ptr_node2->data; } linked_list_ptr_node1 = linked_list_ptr_node2; linked_list_ptr_node2 = linked_list_ptr_node2->next; } linked_list_ptr_node1 = linked_list_ptr_H->next; linked_list_ptr_node2 = linked_list_ptr_H->next->next; } return num; } int LINKED_LIST_PRINTF (linked_list_ptr linked_list_ptr_H) { if (NULL == linked_list_ptr_H) { return -1; } int num = 0; linked_list_ptr linked_list_ptr_node = linked_list_ptr_H; while (NULL != (linked_list_ptr_node = linked_list_ptr_node->next)) { printf ("%d ", linked_list_ptr_node->data); num ++; } if (NULL != linked_list_ptr_H->next) { putchar ('\n'); } return num; } 寻找这段代码的错误
最新发布
12-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值