Fork me on GitHub

PAT(甲级)渡劫(八)-Deduplication on a Linked List(25)


PAT(甲级)渡劫(八)-Deduplication on a Linked List(25)

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

struct node{
int address;
int data;
int next;
}node[100001];

struct node *aqueue[100001],*bqueue[100001];
int arear,afront,brear,bfront; // a,b两个队列的头尾指针
int key[100001];

int main(){
//freopen("in.txt","r",stdin);
int head,n;
scanf("%d %d",&head,&n);
while(n--){
int a,d,nt;
scanf("%d %d %d",&a,&d,&nt);
node[a].address = a;
node[a].data = d;
node[a].next = nt;
}

int x = head;
while(x != -1){
if(!key[abs(node[x].data)]){// 该结点内的值是否出现过,没出现过的保存在aqueue中,出现过的保存在bqueue中
// key[]数组表示该数据是否重复出现
aqueue[arear++] = &node[x];
key[abs(node[x].data)] = 1;
}else{
bqueue[brear++] = &node[x];
}
x = node[x].next; // 指向下一个结点
}
while(afront < arear){
if(afront != arear-1){
printf("%05d %d %05d\n",aqueue[afront]->address,aqueue[afront]->data,aqueue[afront+1]->address);
}else{
printf("%05d %d -1\n",aqueue[afront]->address,aqueue[afront]->data);
}
++afront;
}
while(bfront < brear){
if(bfront != brear-1){
printf("%05d %d %05d\n",bqueue[bfront]->address,bqueue[bfront]->data,bqueue[bfront+1]->address);
}else{
printf("%05d %d -1\n",bqueue[bfront]->address,bqueue[bfront]->data);
}
++bfront;
}
return 0;
}

运行结果:

坚持原创技术分享,您的支持将鼓励我继续创作
-------------本文结束感谢您的阅读-------------
0%