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
参考翻译:
链表由一系列结构组成,这些结构在内存中不一定相邻。我们假设每个结构都包含一个整数和一个指向下一个结构的指针。现在给定一个链表,您应该根据结构的键值以递增的顺序对结构进行排序。keyNext
输入规范:
每个输入文件包含一个测试用例。对于每种情况,第一行都包含正数N (<105) 和头节点的地址,其中N是内存中的节点总数,节点的地址是 5 位正整数。NULL 表示为−1.
然后N接下来是几行,每行描述一个节点,格式如下:
Address Key Next
其中 是内存中节点的地址,是 [AddressKey−105,105],并且是下一个节点的地址。保证所有键都是不同的,并且从头节点开始的链表中没有循环。Next
输出规格:
对于每个测试用例,输出格式与输入格式相同,其中N是列表中节点的总数,所有节点必须按顺序排序。
题解:
静态链表
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100000+1000;
int head;
int n;
struct node
{
int val;
int next;
int flag = 0;
}link[N];
struct node01{
int address;
int val;
int flag = 0;
}sort_link[N];
void sort_link_min(int n,int head){
//int head_now = head;
for(int i = 0, h =head; h != -1; h=link[h].next,i++)
{
sort_link[i].val=link[h].val;
sort_link[i].address=h;
}
// for(int i = 1; i<= n; i++){
// for(int j = i+1; j <= n; j++)
// {
// if(sort_link[j].val < sort_link[i].val)
// {
// swap(sort_link[i].address,sort_link[j].address);
// swap(sort_link[i].val,sort_link[j].val);
// }
// }
// }
}
bool cmp(node01 a,node01 b){
return a.val < b.val;
}
void output(int n){
printf("%d %05d\n",n,sort_link[0].address);
for (int i = 0; i < n-1; i++){
//cout<<sort_link[i].address<<" "<<sort_link[i].val<<" "<<sort_link[i+1].address<<"\n";
printf("%05d %d %05d\n",sort_link[i].address,sort_link[i].val,sort_link[i+1].address);
}
//cout<<sort_link[n].address<<" "<<sort_link[n].val<<" "<<"-1"<<"\n";
printf("%05d %d -1\n",sort_link[n-1].address,sort_link[n-1].val);
}
int main()
{
//输入
scanf("%d%d",&n,&head);
for(int i =1; i <= n; i++){
int address;
cin >>address;
cin>>link[address].val;
cin>>link[address].next;
}
int h = head;int count = 0;
while(h!= -1)
{
count++;
h=link[h].next;
}
sort_link_min(count,head);
sort(sort_link,sort_link+count,cmp);
if (count==0) printf("0 -1\n");
else output(count);
}