您的位置:首页 > 其它

L2-002. 链表去重(用两个队列来储存两个链表)

2018-03-28 18:50 274 查看
题目链接:https://www.patest.cn/contests/gplt/L2-002

思路: 用两个队列来储存两个链表,注意格式输出就好,还要注意不存在 被删除链表的情况

代码:

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>

using namespace std;

const int maxn =100000+5;

struct Edge
{
int key,next;
} e[maxn];

int flag[maxn];

int main()
{
typedef pair<int,int> p;//第一个值为地址,第二个为键值
queue<p>l,l1;
int N,fir_add,address,x,flag1=0;
memset(flag,0,sizeof(flag));
scanf("%d %d",&fir_add,&N);
for(int i=0; i <N; i++)
{
scanf("%d",&address);
scanf("%d %d",&e[address].key,&e[address].next);
}
while(fir_add!=-1)
{
x=abs(e[fir_add].key);
if(flag[x]==0)
{
l.push((p)
{
fir_add,e[fir_add].key
});
flag[x]=1;
}
else
{
l1.push((p)
{
fir_add,e[fir_add].key
});
}
fir_add=e[fir_add].next;
}
p p1;
p1=l.front();
l.pop();
printf("%05d %d ",p1.first,p1.second);
while(!l.empty())
{
p1=l.front();
l.pop();
printf("%05d\n",p1.first);
printf("%05d %d ",p1.first,p1.second);
}
printf("-1\n");
if(!l1.empty())
{
flag1=1;
p1=l1.front();
l1.pop();
printf("%05d %d ",p1.first,p1.second);
}
while(!l1.empty())
{
p1=l1.front();
l1.pop();
printf("%05d\n",p1.first);
printf("%05d %d ",p1.first,p1.second);
}
if(flag1)
printf("-1\n");

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: