-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeduplication on a Linked List.cpp
65 lines (64 loc) · 1.52 KB
/
Deduplication on a Linked List.cpp
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
58
59
60
61
62
63
64
65
#include <algorithm>
#include <cstdio>
#include <iostream>
struct node
{
int adress, date, next;
};
node l[100000];
int visited[100000] = {0};
int flag[100000];
int count = 0;
int main()
{
int adress, n;
std::cin >> adress >> n;
for (int i = 0; i < n; i++)
{
int temp;
std::cin >> temp;
std::cin >> l[temp].date >> l[temp].next;
l[temp].adress = temp;
}
int p = adress;
while (p != -1)
{
if (p == adress)
{
printf("%05d", l[p].adress);
std::cout << " " << l[p].date << " ";
visited[std::abs(l[p].date)] = 1;
}
else
{
if (visited[std::abs(l[p].date)] == 0)
{
printf("%05d", l[p].adress);
std::cout << std::endl;
printf("%05d", l[p].adress);
std::cout << " " << l[p].date << " ";
visited[std::abs(l[p].date)] = 1;
}
else
{
flag[count++] = p;
}
}
p = l[p].next;
}
std::cout << -1 << std::endl;
if (count != 0)
{
printf("%05d", l[flag[0]].adress);
std::cout << " " << l[flag[0]].date << " ";
for (int i = 1; i < count; i++)
{
printf("%05d", l[flag[i]].adress);
std::cout << std::endl;
printf("%05d", l[flag[i]].adress);
std::cout << " " << l[flag[i]].date << " ";
}
std::cout << -1 << std::endl;
}
return 0;
}