L2-022. 重排链表(双队列)
2018-03-12 12:37
375 查看
L2-022. 重排链表
时间限制 500 ms内存限制 65536 kB
代码长度限制 8000 B
判题程序 Standard 作者 陈越
给定一个单链表 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<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<deque>
#include<list>
#include<set>
using namespace std;
typedef long long ll;
const int maxn=1e5+100;
struct node
{
int add;
int id;
int next;
}s[maxn];
node gg[maxn];
node yy[maxn];
int main()
{
int beginn,n;
scanf("%d %d",&beginn,&n);
int x,y,z;
for(int i=1;i<=n;i++){
scanf("%d %d %d",&x,&y,&z);
s[x].add=x;
s[x].id=y;
s[x].next=z;
}
deque<node>qu;
int tt=beginn;
while(tt!=-1){
qu.push_back(s[tt]);
gg[s[tt].id]=s[tt];
tt=s[tt].next;
}
//这个好了 接下来就是说换一下
queue<node>qq;
while(qu.empty()==0){
node tmp=qu.back();
qu.pop_back();
qq.push(tmp);
if(qu.empty()==1)break;
tmp=qu.front();
qu.pop_front();
qq.push(tmp);
}
while(qq.empty()==0){
node tmp=qq.front();
qq.pop();
if(qq.empty()==1)
{
printf("%05d %d %d\n",tmp.add,tmp.id,-1);
break;
}else
{
node a=qq.front();
printf("%05d %d %05d\n",tmp.add,tmp.id,a.add);
}
}
return 0;
}
相关文章推荐
- PAT L2-022 重排链表 (deque双向队列)
- 团体程序设计天梯赛-练习集 -- L2-022. 重排链表
- L2-022. 重排链表(变型)
- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- 重排链表 未完待续 L2-022
- 团体程序设计天梯赛L2-022 重排链表
- PAT 天梯赛 L2-022. 重排链表 【数据结构】
- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- [数据结构]L2-022. 重排链表