-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeLinkedList.cpp
473 lines (451 loc) · 11.5 KB
/
mergeLinkedList.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include <bits/stdc++.h>
using namespace std;
typedef struct node
{
int data;
node *next = NULL;
} node;
void mainMenu()
{
cout << "\n==================MAIN MENU(Link list 1)==================" << endl;
cout << "1. Insert At head" << endl;
cout << "2. Insert before value" << endl;
cout << "3. Insert after value" << endl;
cout << "4. Insert at tail" << endl;
cout << "5. Delete At head" << endl;
cout << "6. Delete before value" << endl;
cout << "7. Delete after value" << endl;
cout << "8. Delete at tail" << endl;
cout << "9. Traverse the link list" << endl;
cout << "10. Count the element" << endl;
cout << "11. Reverse the link List" << endl;
cout << "\n==================MAIN MENU(Link list 2)==================" << endl;
cout << "12. Insert At head" << endl;
cout << "13. Insert before value" << endl;
cout << "14. Insert after value" << endl;
cout << "15. Insert at tail" << endl;
cout << "16. Delete At head" << endl;
cout << "17. Delete before value" << endl;
cout << "18. Delete after value" << endl;
cout << "19. Delete at tail" << endl;
cout << "20. Traverse the link list" << endl;
cout << "21. Count the element" << endl;
cout << "22. Reverse the link List" << endl;
cout << "23. Merge the sorted link List" << endl;
cout << "24. Print merged linkedlist" << endl;
cout << "25. Exit" << endl;
}
int countNodeNumber(node *head)
{
int count = 0;
node *temp = head;
while (temp != NULL)
{
temp = temp->next;
count++;
}
return count;
}
void traverseLL(node *head)
{
node *temp = head;
if (temp == NULL)
{
cout << "Empty linked list";
return;
}
cout << endl;
while (temp != NULL)
{
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL";
}
node *insertAtHead(node *head, int val)
{
node *ptr = new node();
ptr->data = val;
ptr->next = head;
head = ptr;
return head;
}
node *insertBefore(node *head, int key, int val)
{
if (head == NULL)
{
cout << "Linked list is empty so key doesn't exist";
return head;
}
node *temp = head;
node *nodePtr = new node();
nodePtr->data = val;
if (head->data == key)
{
head = insertAtHead(head, val);
return head;
}
while (temp->next != NULL && temp->next->data != key)
temp = temp->next;
if (temp->next == NULL && temp->data != key)
{
cout << "Key doesn't exist";
return head;
}
nodePtr->next = temp->next;
temp->next = nodePtr;
return head;
}
node *insertAfter(node *head, int key, int val)
{
if (head == NULL)
{
cout << "Linked list is empty so key doesn't exist";
return head;
}
node *temp = head;
node *nodePtr = new node();
nodePtr->data = val;
while (temp->data != key && temp->next != NULL)
temp = temp->next;
if (temp->data != key && temp->next == NULL)
{
cout << "Key doesn't exist";
return head;
}
nodePtr->next = temp->next;
temp->next = nodePtr;
return head;
return head;
}
node *insertAtTail(node *head, int val)
{
node *ptr = new node();
if (head == NULL)
{
ptr->data = val;
head = ptr;
}
else
{
node *temp = head;
while (temp->next != NULL)
temp = temp->next;
ptr->data = val;
temp->next = ptr;
}
return head;
}
node *deleteAtHead(node *head)
{
if (head->next == NULL)
{
delete (head);
head = NULL;
return head;
}
node *temp = head->next;
delete (head);
head = temp;
return head;
}
node *deleteBefore(node *head, int key)
{
if (head == NULL)
{
cout << "Can't delete as linked list is empty";
return head;
}
if (head->data == key)
return head;
if (head->next->data == key)
{
head = deleteAtHead(head);
return head;
}
node *currentNodePtr = head->next;
node *prevNodePtr = head;
while (currentNodePtr->next != NULL && currentNodePtr->next->data != key)
{
prevNodePtr = currentNodePtr;
currentNodePtr = currentNodePtr->next;
}
// if (currentNodePtr->next == NULL )
if (currentNodePtr->next == NULL && currentNodePtr->data != key)
{
cout << "Key doesn't exist";
return head;
}
prevNodePtr->next = currentNodePtr->next;
delete (currentNodePtr);
return head;
}
node *deleteAfter(node *head, int key)
{
if (head == NULL)
{
cout << "Can't delete as linked list is empty";
return head;
}
node *temp = head;
while (temp->next != NULL && temp->data != key)
temp = temp->next;
if (temp->next == NULL)
{
if (temp->data != key)
cout << "Key doesn't exist";
else
cout << "Key exist as tail so no node is deleted";
return head;
}
node *temp2 = temp;
temp->next = temp->next->next;
delete (temp2->next);
return head;
}
void deleteTail(node *head)
{
if (head == NULL)
{
cout << "Linked List doesn't exists.";
return;
}
if (head->next == NULL)
head = deleteAtHead(head);
node *temp = head;
while (temp->next->next != NULL)
temp = temp->next;
delete (temp->next);
temp->next = NULL;
}
node *reverseLL(node *head)
{
if (head == NULL)
{
cout << "Can't do the reverse as linked list is empty";
return head;
}
if (head->next == NULL)
return head;
node *prevNode = NULL;
node *currentNode = head;
node *nextNode = NULL;
while (currentNode != NULL)
{
nextNode = currentNode->next;
currentNode->next = prevNode;
prevNode = currentNode;
currentNode = nextNode;
}
head = prevNode;
return head;
}
node *mergeLinklist(node *head1, node *head2)
{
// node *head = new node();
node *head = NULL;
node *temp1 = head1;
node *temp2 = head2;
while (temp1 != NULL && temp2 != NULL)
{
if (temp1->data < temp2->data)
{
head = insertAtTail(head, temp1->data);
temp1 = temp1->next;
}
else
{
head = insertAtTail(head, temp2->data);
temp2 = temp2->next;
}
}
if (temp1 == NULL)
{
while (temp2 != NULL)
{
head = insertAtTail(head, temp2->data);
temp2 = temp2->next;
}
}
else
{
while (temp1 != NULL)
{
head = insertAtTail(head, temp1->data);
temp1 = temp1->next;
}
}
return head;
}
int main()
{
int switchCase = 0;
int in;
node *head = NULL;
node *head2 = NULL;
node *mergedListHead = NULL;
int startLinkSize1 = 0;
int startLinkSize2 = 0;
cout << "Enter the starting linked List 1 size :";
cin >> startLinkSize1;
cout << "Enter the starting linked List 2 size :";
cin >> startLinkSize2;
cout << "Enter the element with space or enter in between them in sorted manner for list 1 :";
int temp;
while (startLinkSize1 != 0)
{
cin >> temp;
head = insertAtTail(head, temp);
startLinkSize1--;
}
cout << "Enter the element with space or enter in between them in sorted manner for list 2 :";
while (startLinkSize2 != 0)
{
cin >> temp;
head2 = insertAtTail(head2, temp);
startLinkSize2--;
}
while (switchCase != 25)
{
mainMenu();
cin >> switchCase;
switch (switchCase)
{
case 1:
cout << "Enter the data value" << endl;
cin >> in;
cout << endl;
head = insertAtHead(head, in);
break;
case 2:
{
int key;
cout << "Enter the data value before which you want to add new node" << endl;
cin >> key;
cout << "Enter the data value" << endl;
cin >> in;
cout << endl;
head = insertBefore(head, key, in);
break;
}
case 3:
{
int key;
cout << "Enter the data value after which you want to add new node" << endl;
cin >> key;
cout << "Enter the data value" << endl;
cin >> in;
cout << endl;
head = insertAfter(head, key, in);
break;
}
case 4:
cout << "Enter the data value" << endl;
cin >> in;
cout << endl;
head = insertAtTail(head, in);
break;
case 5:
head = deleteAtHead(head);
break;
case 6:
cout << "Enter the data value before which node will be deleted" << endl;
cin >> in;
cout << endl;
head = deleteBefore(head, in);
break;
case 7:
cout << "Enter the data value after which node will be deleted" << endl;
cin >> in;
cout << endl;
head = deleteAfter(head, in);
break;
case 8:
deleteTail(head);
break;
case 9:
traverseLL(head);
break;
case 10:
cout << countNodeNumber(head);
break;
case 11:
head = reverseLL(head);
break;
case 12:
cout << "Enter the data value" << endl;
cin >> in;
cout << endl;
head2 = insertAtHead(head2, in);
break;
case 13:
{
int key;
cout << "Enter the data value before which you want to add new node" << endl;
cin >> key;
cout << "Enter the data value" << endl;
cin >> in;
cout << endl;
head2 = insertBefore(head2, key, in);
break;
}
case 14:
{
int key;
cout << "Enter the data value after which you want to add new node" << endl;
cin >> key;
cout << "Enter the data value" << endl;
cin >> in;
cout << endl;
head2 = insertAfter(head2, key, in);
break;
}
case 15:
cout << "Enter the data value" << endl;
cin >> in;
cout << endl;
head2 = insertAtTail(head2, in);
break;
case 16:
head2 = deleteAtHead(head2);
break;
case 17:
cout << "Enter the data value before which node will be deleted" << endl;
cin >> in;
cout << endl;
head2 = deleteBefore(head2, in);
break;
case 18:
cout << "Enter the data value after which node will be deleted" << endl;
cin >> in;
cout << endl;
head2 = deleteAfter(head2, in);
break;
case 19:
deleteTail(head2);
break;
case 20:
traverseLL(head2);
break;
case 21:
cout << countNodeNumber(head2);
break;
case 22:
head2 = reverseLL(head2);
break;
case 23:
mergedListHead = mergeLinklist(head, head2);
break;
case 24:
cout << "Print merged linkedlist";
traverseLL(mergedListHead);
break;
case 25:
switchCase = 25;
break;
default:
break;
}
}
return 0;
}