-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprac.cpp
More file actions
110 lines (102 loc) · 2.02 KB
/
Copy pathprac.cpp
File metadata and controls
110 lines (102 loc) · 2.02 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
Node *creatlink(vector<int> v)
{
Node *head = new Node(-1);
Node *temp = head;
for (int i = 0; i < v.size(); i++)
{
Node *add = new Node(v[i]);
temp->next = add;
temp = add;
}
temp->next = NULL;
head = head->next;
return head;
}
void display(Node *head)
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main()
{
int x, y;
cout << "Enter the size of your linked list 1 & 2: "
<< " ";
cin >> x >> y;
vector<int> A;
vector<int> B;
cout << "\nEnter your linked list A: " << endl;
for (int i = 0; i < x; i++)
{
int a;
cin >> a;
A.push_back(a);
}
cout << "\nEnter your linked list B: " << endl;
for (int i = 0; i < y; i++)
{
int a;
cin >> a;
B.push_back(a);
}
Node *head1 = creatlink(A);
Node *head2 = creatlink(B);
unordered_map<int, int> m;
Node *temp = head1;
Node *C = new Node(-1);
Node *D = new Node(-1);
Node *temp1 = C;
Node *temp2 = D;
while (temp != NULL)
{
if (m[temp->data] == 0)
{
Node *add = new Node(temp->data);
temp2->next = add;
temp2 = add;
}
m[temp->data]++;
temp = temp->next;
}
temp = head2;
while (temp != NULL)
{
if (m[temp->data] > 0)
{
Node *add = new Node(temp->data);
temp1->next = add;
temp1 = add;
}
if (m[temp->data] == 0)
{
Node *add = new Node(temp->data);
temp2->next = add;
temp2 = add;
}
temp = temp->next;
}
C = C->next;
D = D->next;
display(C);
display(D);
}