-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwelfth.cpp
103 lines (103 loc) · 2.02 KB
/
twelfth.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
#include <iostream>
using namespace std;
struct node {
int data;
node* next;
};
node* head1;
node* head2;
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;
}
}
node* merge(node* head1, node* head2) {
node* result = NULL;
if (head1 == NULL) {
return head2;
}
if (head2 == NULL) {
return head1;
}
if (head1->data < head2->data) {
result = head1;
result->next = merge(head1->next, head2);
}
else{
result = head2;
result->next = merge(head1, head2->next);
}
return result;
}
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';
node* res = merge(head1, head2);
while (res != NULL) {
std::cout << res -> data << '\n';
res = res->next;
}
}