-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseventeenth.cpp
111 lines (111 loc) · 2.21 KB
/
seventeenth.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
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
111
#include <iostream>
using namespace std;
struct node {
int data;
node* next;
};
node* head1;
node* head2;
node* head;
node* getNewNode(int x)
{
node* temp = new node();
temp->data = x;
temp->next = NULL;
return temp;
}
void insert1(int x,int n) {
node* temp = getNewNode(x);
node* temp1 = head1;
if (n==1) {
temp->next = head1;
head1 = temp;
}
else
{
for (int i = 0; i < n - 2; i++) {
temp1 = temp1->next;
}
temp->next = temp1->next;
temp1->next = temp;
}
}
void display1() {
node* temp = head1;
while (temp != NULL) {
std::cout << temp->data << '\n';
temp = temp -> next;
}
}
void insert2(int x,int n) {
node* temp = getNewNode(x);
node* temp2 = head2;
if (n==1) {
temp->next = head2;
head2 = temp;
}
else
{
for (int i = 0; i < n - 2; i++) {
temp2 = temp2->next;
}
temp->next = temp2->next;
temp2->next = temp;
}
}
void display2() {
node* temp = head2;
while (temp != NULL) {
std::cout << temp->data << '\n';
temp = temp -> next;
}
}
void common() {
head = new node();
node* temp = head;
node* temp1 = head1;
node* temp2 = head2;
while (temp1 != NULL && temp2 != NULL) {
if (temp1->data == temp2->data) {
temp->next = getNewNode(temp1->data);
temp1=temp1->next;
temp2=temp2->next;
temp = temp->next;
}
else if(temp1->data > temp2->data)
{
temp2 = temp2->next;
}
else{
temp1 = temp1->next;
}
}
}
void display(/* arguments */) {
node* temp = head->next;
while (temp != NULL) {
std::cout << temp->data << '\n';
temp = temp->next;
}
}
int main(int argc, char const *argv[]) {
head1 = NULL;
head2 = NULL;
insert1(2,1);
insert1(4,2);
insert1(6,3);
insert1(8,4);
insert1(10,5);
insert1(12,6);
insert1(14,7);
insert2(1,1);
insert2(2,2);
insert2(3,3);
insert2(4,4);
display1();
std::cout << '\n';
display2();
std::cout << '\n';
common();
display();
}