赞
踩
给定一个单链表 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 (≤
1
0
5
10^5
105)。结点的地址是5位非负整数,NULL地址用−1表示。
接下来有N行,每行格式为:
Address Data Next
其中Address是结点地址;Data是该结点保存的数据,为不超过 1 0 5 10^5 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
/************************************************ ┆ ┏┓ ┏┓ ┆ ┆┏┛┻━━━┛┻┓ ┆ ┆┃ ┃ ┆ ┆┃ ━ ┃ ┆ ┆┃ ┳┛ ┗┳ ┃ ┆ ┆┃ ┃ ┆ ┆┃ ┻ ┃ ┆ ┆┗━┓ ┏━┛ ┆ ┆ ┃ ┃ ┆ ┆ ┃ ┗━━━┓ ┆ ┆ ┃ AC代马 ┣┓┆ ┆ ┃ ┏┛┆ ┆ ┗┓┓┏━┳┓┏┛ ┆ ┆ ┃┫┫ ┃┫┫ ┆ ┆ ┗┻┛ ┗┻┛ ┆ ************************************************ */ #include <iostream> #include <algorithm> #include <set> #include <vector> #include <queue> #include <string> #include <sstream> #include <cstdio> #include <cstring> #include<cctype> #include <math.h> #include <cmath> #include <bits/stdc++.h> #define INF 0x3f3f3f3f #define lb long double #define LINF 0x3f3f3f3f3f3f3f3f #define ull unsigned long long #define random(x) (rand()%x) #define sgn(x) (fabs(x) < eps ? 0 : ((x) < 0 ? -1 : 1)) #define rep(i, a, b) for(int i=a;i<b;i++) #define req(j, a, b) for(int j=a;j<b;j++) #define mem(a, b) memset(a, b, sizeof(a)) #define PI 3.141592653589793 #define K 20 using namespace std; int main() { int a[100000][3],b[2],d[100000],adress,number,link,t,i,c=0; for(int i=0;i<2;i++) { cin>>b[i]; } for(i=0;i<b[1];i++) { cin>>adress>>number>>link; a[adress][0]=adress; a[adress][1]=number; a[adress][2]=link; } int temp=b[0]; for(i=0;a[temp][2]!=-1;i++)//遍历链表,将地址从头到位储存到b数组中 { d[i]=a[temp][0]; temp=a[temp][2]; c++; } d[i]=a[temp][0]; c++; for(int i=0;i<c/2;i++) { a[d[c-i-1]][2]=d[i]; a[d[i]][2]=d[c-i-2]; } if(c%2==0) {a[d[c/2-1]][2]=-1;} else { a[d[b[1]/2]][2]=-1; } t=a[d[c-1]][0]; for(i=0;a[t][2]!=-1;i++) { cout<<setw(5)<<setfill('0')<<a[t][0]<<" "<<a[t][1]<<" "<<setw(5)<<setfill('0')<<a[t][2]<<endl; t=a[t][2]; } cout<<setw(5)<<setfill('0')<<a[t][0]<<" "<<a[t][1]<<" "<<a[t][2]<<endl; return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。