-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlinked_list.cpp
196 lines (152 loc) · 3.47 KB
/
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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
void InsertBegin(Node** head, int data) {
Node* temp = new Node();
temp->data = data;
temp->next = NULL;
if (*head != NULL) temp->next = *head;
*head = temp;
}
void Insert(Node** head, int data, int pos) {
Node* temp1 = new Node();
temp1->data = data;
temp1->next = NULL;
// pos at the beginning
if (pos == 1) {
temp1->next = *head;
*head = temp1;
return;
}
// n'th position
Node* temp2 = *head;
for (int i = 1; i < pos-1; i++) temp2 = temp2->next;
temp1->next = temp2->next;
temp2->next = temp1;
}
Node* Reverse(Node* head) {
Node *current, *prev, *next;
current = head;
prev = next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}
Node* ReverseRecursive(Node* head) {
if (head->next == NULL) {
return head;
}
Node* temp = ReverseRecursive(head->next);
head->next->next = head;
head->next = NULL;
return temp;
}
void Delete(Node** head, int pos) {
Node* temp1 = *head;
// delete first node
if (pos == 1) {
*head = temp1->next;
free(temp1); // de-allocate the memory
return;
}
// n'th position
for (int i = 1; i < pos-1; i++) temp1 = temp1->next;
Node* temp2 = temp1->next;
temp1->next = temp2->next;
free(temp2);
}
void print(Node* temp) {
while (temp == NULL) return;
cout << temp->data << " ";
print(temp->next);
}
void print_recursive(Node* temp) {
while (temp == NULL) return;
print_recursive(temp->next);
cout << temp->data << " ";
}
void printList(Node* head) {
cout << "\n=> ";
print(head);
cout << "\n";
cout << "<= ";
print_recursive(head);
cout << "\n\n";
}
Node* middleNode(Node* head) {
if (head == NULL) {
return head;
}
Node* a = head;
Node* b = head->next;
while (b != NULL && b->next != NULL) {
a = a->next;
b = b->next->next;
}
return a;
}
Node* Merge(Node* a, Node* b) {
Node* temp = new Node();
Node* resultList = temp;
while (a != NULL && b != NULL) {
if (a->data > b->data) {
temp->next = a;
a = a->next;
}
else {
temp->next = b;
b = b->next;
}
temp = temp->next;
}
temp->next = (a == NULL) ? b : a;
return resultList->next;
}
Node* MergeSort(Node* head) {
if (head == NULL || head->next == NULL) {
return head;
}
Node* middle = middleNode(head);
Node* secondHalf = middle->next;
middle->next = NULL; // end of first half
return Merge(MergeSort(head), MergeSort(secondHalf));
}
int main() {
Node* head = NULL;
InsertBegin(&head, 1);
InsertBegin(&head, 5);
InsertBegin(&head, 10);
InsertBegin(&head, 3);
InsertBegin(&head, 21);
printList(head);
Insert(&head, 12, 1);
Insert(&head, 13, 2);
printList(head);
Delete(&head, 2);
Delete(&head, 1);
printList(head);
cout << "Sort:\n";
Node* sorted = MergeSort(head);
printList(sorted);
cout << "Reverse:\n";
Node* rec = ReverseRecursive(head);
printList(rec);
}
/*
=> 2 3 4 5 6
<= 6 5 4 3 2
=> 12 13 2 3 4 5 6
<= 6 5 4 3 2 13 12
=> 2 3 4 5 6
<= 6 5 4 3 2
=> 6 5 4 3 2
<= 2 3 4 5 6
*/