代码实现:
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int Addr, Data, Next;
}linkedlist;
int main()
{
int N, K, S, cnt = 0, count = 0;
linkedlist *dot, *list, *list1;
dot = (linkedlist*)malloc(sizeof(linkedlist) * 100000);
for (int i = 0; i < 100000; i++)
dot[i].Data = -1, dot[i].Next = -1, dot[i].Addr = -1;
scanf("%d %d %d", &S, &N, &K);
list = (linkedlist*)malloc(sizeof(linkedlist)*N);
for (int i = 0; i < N; i++)
{
int addr;
scanf("%d", &addr);
scanf("%d %d", &dot[addr].Data, &dot[addr].Next);
dot[addr].Addr = addr;
}
list[cnt++] = dot[S];
while (list[cnt - 1].Next != -1)
list[cnt] = dot[list[cnt - 1].Next], cnt++;
list1 = (linkedlist*)malloc(sizeof(linkedlist)*cnt);
for (int i = 0; i < cnt; i++)
if (list[i].Data < 0)
list1[count++] = list[i];
for (int i = 0; i < cnt; i++)
if (list[i].Data >= 0 && list[i].Data <= K)
list1[count++] = list[i];
for (int i = 0; i < cnt; i++)
if (list[i].Data > K)
list1[count++] = list[i];
for (int i = 0; i < count - 1; i++)
printf("%05d %d %05d\n", list1[i].Addr, list1[i].Data, list1[i + 1].Addr);
printf("%05d %d -1", list1[count - 1].Addr, list1[count - 1].Data);
return 0;
}