这种链表貌似叫静态链表……
#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
typedef struct Link
{
int adress, data, next;
int order;//原顺序
} Link;
Link L[100010] = {0}, T[100010] = {0};//L以地址存放,T以顺序存放
int main()
{
int i, first, temp, adress, N, K, cnt = 0;
scanf("%d %d %d", &first, &N, &K);
for(i=0; i<N; i++)
{
scanf("%d", &adress);
scanf("%d %d", &L[adress].data, &L[adress].next);
L[adress].adress = adress;//按地址存放
}
for(adress = first; adress != -1; adress = L[adress].next)//按地址串顺序放入T
{
T[cnt] = L[adress];
T[cnt++].order = cnt;
}
for(i=0; i+K<=cnt; i+=K) reverse(T+i, T+i+K);//翻转
T[cnt].adress = -1;//最后一个的next指向T[cnt].adress
for(i=0; i<cnt; i++)
{
T[i].next = T[i+1].adress;//修改next
printf("%05d %d ", T[i].adress, T[i].data);
if(T[i].next == -1) printf("-1\n");//-1的输出要单独考虑
else printf("%05d\n", T[i].next);
}
return 0;
}