输入样例:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
输出样例:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
参考答案:
#include<iostream>
using namespace std;
struct node{
int data;
int next;
};
int main()
{
int L,n,k;
node nodearray[100001];
//创建链表
cin >> L >> n >> k;
int next, adress, data;
for (int i = 0; i < n; i++)
{
cin >> adress >> data >> next;
nodearray[adress].data = data;
nodearray[adress].next = next;
}
nodearray[100000].next = L;
int r = L, t = 100000, w = L, q = L, p = -1;
n = 0;
while (r != -1)
{
r = nodearray[r].next;
n++;
if (n % k == 0)
{
p = w;
while (p != r)
{
q = nodearray[p].next;
nodearray[p].next = nodearray[t].next;
nodearray[t].next = p;
p = q;
}
nodearray[w].next = r;
t = w;
w = nodearray[t].next;
}
}
//输出链表
p = nodearray[100000].next;
while (p != -1)
{
if (nodearray[p].next != -1)
{
printf("%05d %d %05d\n", p, nodearray[p].data, nodearray[p].next);
}
else
{
printf("%05d %d %d\n", p, nodearray[p].data, -1);
}
p = nodearray[p].next;
}
}