用手机做网站,国外做网站,微信开发者版是什么意思,wordpress上传图片教程给定一个单链表 L1→L2→⋯→Ln−1→Ln#xff0c;请编写程序将链表重新排列为 Ln→L1→Ln−1→L2→⋯。例如#xff1a;给定L为1→2→3→4→5→6#xff0c;则输出应该为6→1→5→2→4→3。
输入格式#xff1a;
每个输入包含1个测试用例。每个测试用例…
给定一个单链表 L1→L2→⋯→Ln−1→Ln请编写程序将链表重新排列为 Ln→L1→Ln−1→L2→⋯。例如给定L为1→2→3→4→5→6则输出应该为6→1→5→2→4→3。
输入格式
每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址和结点总个数即正整数N (≤105)。结点的地址是5位非负整数NULL地址用−1表示。
接下来有N行每行格式为
Address Data Next其中Address是结点地址Data是该结点保存的数据为不超过105的正整数Next是下一结点的地址。题目保证给出的链表上至少有两个结点。
输出格式
对每个测试用例顺序输出重排后的结果链表其上每个结点占一行格式与输入相同。
输入样例
00100 6
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218输出样例
68237 6 00100
00100 1 99999
99999 5 12309
12309 2 00000
00000 4 33218
33218 3 -1
#include iostream
#include vector
#include set
#include string
#include iomanip
#include algorithm
using namespace std;
#define M 100000
//00100 6
//00000 4 99999
//00100 1 12309
//68237 6 - 1
//33218 3 00000
//99999 5 68237
//12309 2 33218
int val[M 5] { 0 }, nex[M 5] { 0 }, pre[M 5] { 0 };
int main() {int root, n, end;cin root n;for (int i 0, a, b, c; i n; i) {cin a b c;val[a] b;nex[a] c;if (c ! -1) pre[c] a;if (c -1) end a;}while (1) {printf(%05d %d , end, val[end]);if (root end) {cout -1;return 0;}else printf(%05d\n, root);end pre[end];printf(%05d %d , root, val[root]);if (root end) {cout -1;return 0;}else printf(%05d\n, end);root nex[root];}return 0;
}