-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_linked_list.cpp
More file actions
416 lines (367 loc) · 9.2 KB
/
single_linked_list.cpp
File metadata and controls
416 lines (367 loc) · 9.2 KB
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
#include <iostream>
#include "single_linked_list.h"
bool isEmpty(NPTR &list)
{
if(list == nullptr)
return true;
return false;
}
NPTR createNode(const int &value)
{
NPTR plist = new NODE;
plist->info = value;
plist->next = nullptr;
return plist;
}
void insertFront(NPTR &list, const int &value, NPTR &lastNode)
{
NPTR newNode = createNode(value);
if(isEmpty(list))
{
list = newNode;
lastNode = newNode;
return;
}
newNode->next = list;
list = newNode;
}
void insertEnd(NPTR &list, const int &value, NPTR &lastNode)
{
NPTR newNode = createNode(value);
if(isEmpty(list))
{
lastNode = newNode;
list = newNode;
return;
}
lastNode->next = newNode;
lastNode = newNode;
}
int countNode(NPTR list)
{
int count = 1;
while(list != nullptr)
{
count++;
list = list->next;
}
return count - 1;
}
/*
void concatList(NPTR &firstList, NPTR &secondList, NPTR &firstLastNode, NPTR &secondLastNode)
{
int firstval, secondval = 0;
std::cout << "Please enter the number of elements of first list" << std::endl;
std::cin >> firstListEl;
std::cout << "Please enter the number of elements of second list" << std::endl;
std::cin >> secondListEl;
if(firstList == nullptr && secondList == nullptr)
{
std::cout << "Lists are empty" << std::endl;
return;
}
for(int i = 0; i < firstListEl; i++)
{
insertEnd(firstList, value,
std::cout << "~Second List~" << std::endl;
insertEnd(secondlist, value, secondLastNode);
firstLastNode->next = secondList;
firstLastNode = secondLastNode;
std::cout << "The appended list is" << firstList << std::endl;
}
*/
void delFirst(NPTR &list)
{
NPTR delNode = nullptr;
if(isEmpty(list))
{
std::cout << "Empty List! Nothing to delete" << std::endl;
return;
}
if(countNode(list) == 1)
{
delete list;
list = nullptr;
return;
}
delNode = list;
list = list->next;
delete delNode;
delNode = nullptr;
}
void delEnd(NPTR &list, NPTR &lastNode)
{
NPTR delNode = nullptr;
NPTR bak = list;
if(isEmpty(list))
{
std::cout << "Empty List! Nothing to delete" << std::endl;
return;
}
if(countNode(list) == 1)
{
delete list;
list = nullptr;
return;
}
while(list->next != lastNode)
{
list = list->next;
}
list->next = nullptr;
delNode = lastNode;
lastNode = list;
delete delNode;
delNode = nullptr;
list = bak;
}
NPTR copyList(NPTR list)
{
if(isEmpty(list))
{
std::cout << "Source list is empty!" << std::endl;
return list;
}
const int head = list->info;
NPTR secondList = createNode(head);
if(list->next != nullptr)
secondList->next = copyList(list->next);
return secondList;
}
int searchEl(NPTR list, const int &value)
{
int count = 1;
if(isEmpty(list))
return 0;
if(countNode(list) == 1)
return 1;
while(list->info != value)
{
count++;
list = list->next;
}
return count++;
}
void insertPos(NPTR list, const int &value, const int &pos)
{
int count = 1;
NPTR newNode = createNode(value);
NPTR backup = nullptr;
NPTR prevNode = nullptr;
if(isEmpty(list))
std::cout << " Element cannot be inserted. Empty List!" << std::endl;
while(count != pos - 1)
{
count++;
list = list->next;
}
prevNode = list->next;
list->next = newNode;
newNode->next = prevNode;
}
void insertAfterx(NPTR list, const int &key, const int &value)
{
NPTR newNode = createNode(value);
NPTR temp = nullptr;
if(isEmpty(list))
std::cout << "Element cannot be inserted.List is Empty!" << std::endl;
while(list->info != key)
{
list = list->next;
}
temp = list->next;
list->next = newNode;
newNode->next = temp;
}
void delPos(NPTR list, const int &pos)
{
int count = 1;
NPTR nextNode = nullptr;
NPTR delNode = nullptr;
if(isEmpty(list))
std::cout << " Element cannot be deleted. Empty List!" << std::endl;
while(count != pos - 1)
{
count++;
list = list->next;
}
delNode = list->next;
nextNode = delNode->next;
list->next = nextNode;
delete delNode;
delNode = nullptr;
}
void deleteAfterx(NPTR list, const int &key)
{
int counter = countNode(list);
int valPos = searchEl(list, key);
const int newPos = valPos + 1;
if(isEmpty(list))
std::cout << "Element cannot be deleted. Empty list!" << std::endl;
if(newPos == -1 || newPos > counter)
std::cout << "Invalid Position!" << std::endl;
delPos(list, newPos);
}
void swapEl(NPTR &list, const int &firstEl, const int &secondEl)
{
int firstCount, secondCount = 1;
int firstPos = searchEl(list, firstEl);
int secondPos = searchEl(list, secondEl);
NPTR firstPointer = list;
NPTR secondPointer = list;
while(firstCount != firstPos - 1)
{
firstPointer = firstPointer->next;
firstCount++;
}
while(secondCount != secondPos)
{
secondPointer = secondPointer->next;
secondCount++;
}
std::cout << secondPointer->info;
int tmp = firstPointer->info;
firstPointer->info = secondPointer->info;
secondPointer->info = tmp;
}
NPTR revList(NPTR list)
{
NPTR prevNode = nullptr;
NPTR currentNode = list;
NPTR revList = list;
NPTR bak = nullptr;
while(currentNode != nullptr && revList != nullptr)
{
bak = currentNode;
currentNode = currentNode->next;
revList->next = prevNode;
prevNode = bak;
revList = currentNode;
}
revList = prevNode;
return revList;
}
void display(NPTR list)
{
if(isEmpty(list))
{
std::cout << "List is empty" << std::endl;
return;
}
while(list != nullptr)
{
std::cout << list->info << " ";
list = list->next;
}
std::cout << "\n";
}
void delList(NPTR &list)
{
NPTR delNode = nullptr;
if(isEmpty(list))
return;
while(list != nullptr)
{
delNode = list;
list = list->next;
delete delNode;
}
list = nullptr;
}
int main()
{
int ch, val, nodeCount, pos, key, firstElement, secondElement, firstListEl, secondListEl = 0;
NPTR list = nullptr;
NPTR lastNode = nullptr;
NPTR secondList = nullptr;
NPTR firstLastNode = nullptr;
NPTR secondLastNode = nullptr;
NPTR reverse = nullptr;
NPTR copy = nullptr;
do
{
std::cout << "~~~~~~~~~~~~~MENU~~~~~~~~~~~~~" << std::endl;
std::cout << "1. Insert at front" << std::endl;
std::cout << "2. Insert at last" << std::endl;
std::cout << "3. Count the number of nodes" << std::endl;
//std::cout << "4. Append two linked lists" << std::endl;
std::cout << "5. Delete from front" << std::endl;
std::cout << "6. Delete at end" << std::endl;
std::cout << "7. Search for an element in list" << std::endl;
std::cout << "8. Insert at any position in list" << std::endl;
std::cout << "9. Insert after an element in linked list" << std::endl;
std::cout << "10. Delete from any position in list" << std::endl;
std::cout << "11. Delete after an element in linked list" << std::endl;
std::cout << "12. Swap two data in a node" << std::endl;
std::cout << "13. Reverse the linked list" << std::endl;
std::cout << "14. Display the list" << std::endl;
std::cout << "15. Copy a linked list to another list" << std::endl;
std::cout << "16. Exit" << std::endl;
std::cout << "Please enter your choice" << std::endl;
std::cin >> ch;
switch(ch)
{
case 1 : std::cout << "Please enter the value you want to enter" << std::endl;
std::cin >> val;
insertFront(list, val, lastNode);
break;
case 2 : std::cout << "Please enter the value you want to enter" << std::endl;
std::cin >> val;
insertEnd(list, val, lastNode);
break;
case 3 : nodeCount = countNode(list);
std::cout << " The number of nodes in the linked list are " << nodeCount << std::endl;
break;
/*case 4 :
std::cout << "The appended list is" << concatList(list, secondList, firstLastNode, secondLastNode);
break;
*/
case 5 : delFirst(list);
break;
case 6 : delEnd(list, lastNode);
break;
case 7 : std::cout << "Please enter the value you want to search for" << std::endl;
std::cin >> val;
pos = searchEl(list, val);
std::cout << "The element is found at position" << pos << "." << std::endl;
break;
case 8 : std::cout << "Please enter the element you want to insert along with the position" << std::endl;
std::cin >> val;
std::cin >> pos;
insertPos(list, val, pos);
break;
case 9 : std::cout << "Please enter the value of the node" << std::endl;
std::cin >> key;
std::cout << "Please enter the value to insert" << std::endl;
std::cin >> val;
insertAfterx(list, key, val);
break;
case 10 : std::cout << "Please enter the position of deletion" << std::endl;
std::cin >> pos;
delPos(list, pos);
break;
case 11 : std::cout << "Please enter the value of the node" << std::endl;
std::cin >> key;
deleteAfterx(list, key);
break;
case 12 : std::cout << "Please enter the two values you want to swap" << std::endl;
std::cin >> firstElement >> secondElement;
swapEl(list, firstElement, secondElement);
break;
case 13 : reverse = revList(list);
display(reverse);
delList(reverse);
break;
case 14 : display(list);
break;
case 15 : copy = copyList(list);
display(copy);
delList(copy);
break;
case 16 : delList(list);
break;
default : std::cout << "Invalid Choice" << std::endl;
}
}while(ch != 16);
std::cout << "Thank You !" << std::endl;
}