这道题和1074差不多,在一个链表上进行排序
1.首先将在一个链表的分离出来
2.排序
3**注意判断输入为0和输入head不存在的情况**
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
struct p
{
int add;
int Key;
int next;
}node[100004];
bool cmp(p x,p y)
{
return x.Key < y.Key;
}
int main()
{
int n,head;
while(scanf("%d%d",&n,&head) != EOF)
{
p ListNode[100004];
int cnt = 0;
for(int i = 0; i < n; i++)
{
int x;
scanf("%d",&x);
node[x].add = x;
scanf("%d%d",&node[x].Key,&node[x].next);
}
while(head != -1)//将在一个链表的节点取出
{
if(head == node[head].add)
{
ListNode[cnt++] = node[head];
head = node[head].next;
}else
break;
}
if(cnt == 0)//判断0的情况
{
printf("0 -1\n");
continue;
}
sort(ListNode,ListNode+cnt,cmp);//排序
printf("%d %05d\n",cnt,ListNode[0].add);
for(int i = 0; i < cnt-1; i++)
printf("%05d %d %05d\n",ListNode[i].add,ListNode[i].Key,ListNode[i+1].add);
printf("%05d %d -1\n",ListNode[cnt-1].add,ListNode[cnt-1].Key);
}
return 0;
}