-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayWithLL.cpp
431 lines (359 loc) · 8.65 KB
/
playWithLL.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
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int value;
Node *next;
Node(int val)
{
this->value = val;
this->next = NULL;
}
};
// get LL size
int sizeOfLL(Node *head)
{
Node *tmp = head;
int count = 1;
while (tmp->next != NULL)
{
tmp = tmp->next;
count++;
}
return count;
}
// insert element on the linked list
void insertElement(Node *&head, int value)
{
Node *newNode = new Node(value);
Node *tmp = head;
if (tmp == NULL)
{
head = newNode;
return;
}
while (tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = newNode;
}
// insert value at any position
void pushElement(Node *&head, int value, int pos)
{
Node *newNode = new Node(value);
Node *tmp = head;
if (head == NULL)
{
head = newNode;
return;
}
if (pos == 1)
{
newNode->next = tmp;
head = newNode;
return;
}
if (pos - 1 == sizeOfLL(head))
{
insertElement(head, value);
return;
}
pos--;
for (int i = 1; i < pos; i++)
{
tmp = tmp->next;
}
Node *nextTmpNode = tmp->next;
tmp->next = newNode;
newNode->next = nextTmpNode;
}
// search value from a unique linked list
int searchValueFromUniqueLL(Node *head, int key)
{
Node *tmp = head;
int count = 1;
if (tmp == NULL)
{
cout << "Linked list is empty" << endl;
return -1;
}
while (tmp->value != key)
{
if (tmp->next == NULL)
{
cout << "Value not found" << endl;
return -1;
}
tmp = tmp->next;
count++;
}
return count;
}
// search a value from LL, which contain duplicate value
void searchValueFromLLContainedDuplicate(Node *head, int key)
{
Node *tmp = head;
int count = 1;
int flag = 0;
while (tmp != NULL)
{
if (tmp->value == key)
{
cout << count << " ";
flag++;
}
tmp = tmp->next;
count++;
}
if (flag == 0)
cout << "Value is not found" << endl;
}
// Update value at any position
void updateValueAtSpecificPosition(Node *head, int value, int pos)
{
Node *tmp = head;
for (int i = 1; i < pos; i++)
{
tmp = tmp->next;
}
tmp->value = value;
}
// delete value from at any position
void deleteNodeAtSpecificPosition(Node *&head, int pos)
{
Node *tmp = head;
if (pos == 0)
{
head = tmp->next;
delete tmp;
return;
}
for (int i = 0; i < pos - 1; i++)
{
tmp = tmp->next;
}
Node *nextOfDeletedNode = tmp->next->next;
delete tmp->next;
tmp->next = nextOfDeletedNode;
}
// delete by value from LL
void deleteByValueOnUniqueLL(Node *&head, int value)
{
int position = searchValueFromUniqueLL(head, value);
// cout << "pos " << position << endl;
--position;
deleteNodeAtSpecificPosition(head, position);
}
// Get middle element of the LL
void getMid(Node *head)
{
Node *fastOne = head;
Node *slowOne = head;
if (head != NULL)
{
while (fastOne != NULL && fastOne->next != NULL)
{
fastOne = fastOne->next->next;
slowOne = slowOne->next;
}
cout << "The middle element is : " << slowOne->value << endl;
}
}
// Read all value
void printLL(Node *n)
{
// cout << n->value;
while (n != NULL)
{
cout << n->value;
if (n->next != NULL)
{
cout << " ";
}
n = n->next;
}
cout << "\n\n";
}
// print in reverse way (non recursive way)
Node *reverseNonRec(Node *&head)
{
Node *prevNode = NULL;
Node *currentNode = head;
if (head == NULL)
{
cout << "The LL is empty" << endl;
return head;
}
Node *nextNode = head->next;
while (true)
{
currentNode->next = prevNode;
prevNode = currentNode;
currentNode = nextNode;
if (currentNode == NULL)
{
break;
}
nextNode = nextNode->next;
}
cout << "Reverse successfully" << endl;
return prevNode;
}
// print LL in reverse way (using recursive concept)
Node *reverseLLRecursive(Node *&head)
{
// Base call
if (head == NULL || head->next == NULL)
{
if (head == NULL)
cout << "The LL is empty" << endl;
return head;
}
// recursive call
Node *newHead = reverseLLRecursive(head->next);
head->next->next = head;
head->next = NULL;
return newHead;
}
// sorting the linked list using bubble sort algorithm. Time complexity O(n^2)
void sortTheLL(Node *&head)
{
Node *current = head, *index = NULL;
int tmp;
if (head == NULL)
{
cout << "Linked list is empty";
return;
}
else
{
while (current != NULL)
{
index = current->next;
while (index != NULL)
{
if (current->value > index->value)
{
tmp = current->value;
current->value = index->value;
index->value = tmp;
}
index = index->next;
}
current = current->next;
}
}
}
int main()
{
Node *head = NULL;
int val, pos;
cout << "Choice 1. for insert Value"
<< "\n"
<< "Choice 2. for insert Value on specific position"
<< "\n"
<< "Choice 3. for delete value at specific position"
<< "\n"
<< "Choice 4. which value you wanna delete"
<< "\n"
<< "Choice 5. for update value "
<< "\n"
<< "Choice 6. for Read All Value "
<< "\n"
<< "Choice 7. for search a Value from LL contain unique value "
<< "\n"
<< "Choice 8. for search a Value from LL contain duplicate "
<< "\n"
<< "Choice 9. to get the size of LL"
<< "\n"
<< "Choice 10. to get the reverse LL"
<< "\n"
<< "Choice 11. to get the Middle element"
<< "\n"
<< "Choice 12. to sort the linked list"
<< "\n"
<< "choice 0. for Exit"
<< "\n\n";
int choice = 1;
cout << "Alert! initially insert 5-10 value then apply other operation\n\n";
while (choice <= 12 && choice > 0)
{
switch (choice)
{
case 1:
cout << "Enter the value to insert: ";
cin >> val;
insertElement(head, val);
break;
case 2:
cout << "Enter the value and pos: ";
cin >> val >> pos;
// call the insert function
pushElement(head, val, pos);
break;
case 3:
cout << "Enter the position: ";
cin >> pos;
// call the delete function
deleteNodeAtSpecificPosition(head, pos - 1);
break;
case 4:
cout << "Enter a value you wanna delete: ";
cin >> val;
// call the update function
deleteByValueOnUniqueLL(head, val);
break;
case 5:
cout << "Enter the value and position: ";
cin >> val >> pos;
// call the update function
updateValueAtSpecificPosition(head, val, pos);
break;
case 6:
cout << "Read all the values: ";
// call the Read function
printLL(head);
break;
case 7:
cout << "Enter the value: ";
cin >> val;
// call the searchValueFromUniqueLL function
;
cout << "Value found at " << searchValueFromUniqueLL(head, val) << " number position" << endl;
break;
case 8:
cout << "Enter the value: ";
cin >> val;
// searching func for duplicate value
cout << "The number is at position: ";
searchValueFromLLContainedDuplicate(head, val);
cout << endl;
break;
case 9:
cout << "The size of LL : " << sizeOfLL(head) << endl;
break;
case 10:
// head = reverseNonRec(head);
head = reverseLLRecursive(head);
break;
case 11:
getMid(head);
case 12:
sortTheLL(head);
cout << "Linked list is now sorted" << endl;
default:
break;
}
cout << "Enter your choice: ";
cin >> choice;
}
// print the list after end of the operation
if (head != NULL)
{
cout << "your LL : ";
printLL(head);
}
return 0;
}