-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingly_linked_list.cpp
408 lines (377 loc) · 8.25 KB
/
singly_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
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
#include <bits/stdc++.h>
using namespace std;
typedef struct Node
{
int key;
Node *next;
} Node;
//first node is the head node
//last node is the tail node
// Head pointer always points to first element of the linked list
Node *head = NULL;
Node *createNode(int data)
{
Node *new_node = new Node; // step 1: memory allocation
new_node->key = data; // step 2: data allocation
new_node->next = NULL; // step 3: next node pointer
return new_node;
}
int getSize()
{
Node *temp = head;
int count = 0;
//1.traverse to the end of the list
while (temp != NULL)
{
temp = temp->next;
count++;
}
//2.point the last node to new node
return count;
}
//insert_begin can be called as prepend
void insert_begin(int val)
{
//1.createNode
Node *new_node = createNode(val);
//if head == NULL, the ll is empty
if (head == NULL)
{
head = new_node;
}
else
{
//1.first point the new node to the node that is pointed by head
new_node->next = head;
//2.then point the head to the new node
head = new_node;
}
return;
}
void insertAfterNode(Node *prev, int val)
{
Node *new_node = createNode(val);
if (prev == NULL)
{
cout << "the given node cannot point to NULL" << endl;
}
else
{
new_node->next = prev->next;
prev->next = new_node;
}
return;
}
//note that we cannot insert at last position using insertAt
//we cannot insert at the first position using insertAt if the list is empty or head=null
void insertAt(int pos, int val)
{
if (head == NULL)
{
cout << "the linked list is empty" << endl;
return;
}
Node *new_node = createNode(val);
Node *temp = head;
if (pos == 1)
{
insert_begin(val);
return;
}
else
{
for (int i = 1; i <= pos; i++)
{
if (temp == NULL)
{
printf("Insertion is not possible\n");
}
if (i == pos - 1)
{
new_node->next = temp->next;
temp->next = new_node;
return;
}
else
{
temp = temp->next;
}
}
}
}
//insertEnd can be called append
void insertEnd(int val)
{
Node *new_node = createNode(val);
//if the linked list is empty , then the head pointer will be the tail pointer itself
if (head == NULL)
{
insert_begin(val);
}
else
{
//1.traverse to the end of the list
Node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
//2.point the last node to new node
temp->next = new_node;
}
return;
}
void sort()
{
int swapped;
Node *curr;
if (head == NULL)
{
return;
}
do
{
/* code */
swapped = 0;
curr = head;
while (curr->next != NULL)
{
if (curr->key > curr->next->key)
{
swap(curr->key, curr->next->key);
swapped = 1;
}
curr = curr->next;
}
} while (swapped == 1);
}
int SearchKey(int val)
{
Node *temp = head;
int pos = 1;
while (temp != NULL)
{
if (temp->key == val)
break;
temp = temp->next;
pos++;
}
if (temp == NULL)
{
cout << "not found" << endl;
}
else
{
cout << "found the value " << val << " at " << pos << endl;
}
return pos;
}
int SearchAt(int pos)
{
Node *temp = head;
if (temp != NULL)
{
for (int i = 1; i < pos; i++)
{
temp = temp->next;
}
}
else
{
cout << "The list is empty" << endl;
}
return temp->key;
}
void DeleteBegin()
{
Node *temp = head;
head = head->next;
free(temp);
cout << "Successfully deleted the first node" << endl;
}
void DeleteEnd()
{
Node *prev, *curr;
curr = head;
while (curr->next != NULL)
{
prev = curr;
curr = curr->next;
}
free(curr);
prev->next = NULL;
cout << "Successfully deleted the last node" << endl;
}
void DeleteAt(int pos)
{
if (pos == 1)
{
DeleteBegin();
}
else if (head == NULL)
{
cout << "the list is empty" << endl;
return;
}
else
{
Node *temp = head;
Node *prev = head;
for (int i = 1; i < pos; i++)
{
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
cout << "successfully deleted " << temp->key << " at pos " << pos << endl;
free(temp);
}
}
void DeleteKey(int val)
{
int pos = SearchKey(val);
DeleteAt(pos);
}
void reverse()
{
Node *curr, *prev, *next;
curr = head;
prev = NULL;
next = NULL;
while (curr != NULL)
{
//make the next pointer point towards curr->next
next = curr->next;
//rotate the curr->next to prev
curr->next = prev;
//now shift the prev to next node
//remember the sequence-> prev->curr->next
prev = curr;
//and shift the curr to next node
curr = next;
}
//shift the head to the last node which is now prev as
//curr and next would be pointing to NULL now
head = prev;
}
void printList()
{
Node *temp;
temp = head;
while (temp != NULL)
{
cout << temp->key << " ";
temp = temp->next;
}
cout << endl;
}
void freeTheList()
{
Node *temp = head;
while (head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
}
int main()
{
int n;
cout << "enter the size of the linked list: \n";
cin >> n;
int data;
for (int i = 0; i < n; i++)
{
cin >> data;
Node *new_node = createNode(data);
if (head == NULL)
{
head = new_node;
}
else
{
Node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = new_node;
}
}
cout << "enter\n0 -> break\n1 -> InsertBegin\n2 -> InsertAt\n3 -> InsertEnd\n4 -> InsertAfterNode\n5->sort\n6--> reverse\n";
int choice;
while (1)
{
cin >> choice;
if (choice == 1)
{
cout << "insert a value at first position: " << endl;
int inp;
cin >> inp;
insert_begin(inp);
printList();
}
else if (choice == 2)
{
cout << "insert a value at random position: \ninsert the position" << endl;
int pos, inp;
cin >> pos >> inp;
int size = getSize();
cout << "the size of the linked list is " << size << "\n";
if (pos < 1 || pos >= size + 1)
{
cout << "invalid position" << endl;
}
else
{
insertAt(pos, inp);
printList();
}
}
else if (choice == 3)
{
cout << "insert a value at end position: " << endl;
int inp;
cin >> inp;
insertEnd(inp);
printList();
}
else if (choice == 4)
{
if (head == NULL)
{
cout << "the given node cannot point to NULL" << endl;
}
else
{
cout << "insert a value after a given node: " << endl;
int inp;
cin >> inp;
//note that you have to ensure that is head->next is null it will run fine as a condition is there to
//handle it but can't pass head->next->next as it is not null and hence the program will crash
insertAfterNode(head->next, inp);
printList();
}
}
else if (choice == 0)
{
break;
}
else if (choice == 5)
{
sort();
printList();
}
else if(choice == 6)
{
reverse();
printList();
}
else
{
cout << "invalid choice" << endl;
}
}
freeTheList();
}