#include<iostream>
using namespace std;
#include<map>
#include<stack>
struct node
{
int address;
int value;
};
node arr[100000];
int main()
{
map<int, int> m1, m2;
int fa, length, rl;
cin >> fa >> length >> rl;
for (int i = 0;i < length;i++)
{
int a, b, c;
cin >> a >> b >> c;
m1[a] = b;
m2[a] = c;
}
int length1 = 0;
for (int i = 0;i < length;i++)
{
arr[i].address = fa;
arr[i].value = m1[fa];
fa = m2[fa];
length1++;
if (fa == -1)break;
}
stack<node> s1;int count = 0;
for (int i = 0;i < length1;i++)
{
if (count + rl > length1)
{
break;
}
for (int j = count;j < count + rl;j++)
{
s1.push(arr[j]);
}
for (int j = count;j < count + rl;j++)
{
arr[j] = s1.top();
s1.pop();
}
count = count + rl;
}
for (int i = 0;i < length1;i++)
{
printf("%05d %d ", arr[i].address, arr[i].value);
if (i + 1 == length1)
{
printf("-1\n");
}
else
{
printf("%05d\n", arr[i + 1].address);
}
}
}
1,数组模拟链表存储,如何完成存储
2,用stack进行反转操作