-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayWithDoublyLL.cpp
339 lines (288 loc) · 6.63 KB
/
playWithDoublyLL.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
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int value;
Node *next;
Node *prev;
Node(int val)
{
value = val;
next = NULL;
prev = NULL;
}
};
// store array
struct storeArr
{
int position[1000]; // Its occupied only thousand value
};
// get the size of linked list
int sizeOfLL(Node *head)
{
Node *tmp = head;
int count = 0;
while (tmp != NULL)
{
count++;
tmp = tmp->next;
}
return count;
}
// add element at the tail
void addElement(Node *&head, int value)
{
Node *newNode = new Node(value);
if (head == NULL)
{
head = newNode;
return;
}
Node *tmp = head;
while (tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = newNode;
newNode->prev = tmp;
}
// push element on the lined list
void push(Node *&head, int value, int pos)
{
// create new node to append on parent linked list
Node *newNode = new Node(value);
// check if head is null or not
if (head == NULL)
{
head = newNode;
return;
}
// check, is user wanna change the head node
if (pos == 1)
{
newNode->next = head;
head->prev = newNode;
head = newNode;
return;
}
// check is user wanna insert a value out of the box
if (pos > sizeOfLL(head))
{
cout << "You cant insert element out of the linked list size" << endl;
return;
}
// create temporary variable
Node *tmp = head;
// traverse the list till before deleted node found
for (int i = 1; i < pos - 1; i++)
{
tmp = tmp->next;
}
// change the node connection
Node *storeNext = tmp->next;
tmp->next = newNode;
newNode->next = storeNext;
newNode->prev = tmp;
storeNext->prev = newNode;
}
// update value at any given position
void updateValue(Node *head, int value, int position)
{
if (head == NULL)
{
cout << "Linked list is empty" << endl;
return;
}
Node *tmp = head;
for (int i = 1; i < position; i++)
{
tmp = tmp->next;
}
tmp->value = value;
}
// search value from linked list
storeArr searchValue(Node *head, int key)
{
Node *tmp = head;
int count = 1;
storeArr arr;
int k = 1;
while (tmp != NULL)
{
if (tmp->value == key)
{
arr.position[k] = count;
k++;
}
tmp = tmp->next;
count++;
}
arr.position[0] = k;
return arr;
}
void deleteValue(Node *&head, int value)
{
Node *tmp = head;
while (tmp != NULL)
{
if (tmp->value == value)
{
if (tmp == head)
{
if (tmp->next == NULL)
{
head = NULL;
cout << "Your linked list is now empty" << endl;
}
else
{
Node *headNext = tmp->next;
headNext->prev = NULL;
head = headNext;
}
}
else if (tmp->next == NULL)
{
Node *tmpPrev = tmp->prev;
tmpPrev->next = NULL;
}
else
{
Node *tmpPrev = tmp->prev;
Node *tmpNext = tmp->next;
tmpPrev->next = tmpNext;
tmpNext->prev = tmpPrev;
}
}
tmp = tmp->next;
}
}
// reverse the linked list
void reverse(Node *&head)
{
Node *tmp = NULL;
Node *current = head;
if (head == NULL)
{
cout << "The LL is empty" << endl;
return;
}
while (current != NULL)
{
tmp = current->prev;
current->prev = current->next;
current->next = tmp;
current = current->prev;
}
head = tmp->prev;
}
// print the linked list
void print(Node *n)
{
if (n == NULL)
{
cout << "empty" << endl;
return;
}
while (n != NULL)
{
cout << n->value;
if (n->next != NULL)
{
cout << " -> ";
}
n = n->next;
}
cout << endl;
}
int main()
{
Node *head = NULL;
cout << "Play with doubly linked list: "
<< "\n\n"
<< "Enter 1 to insert a value"
<< "\n"
<< "Enter 2 to push a value"
<< "\n"
<< "Enter 3 to update a value"
<< "\n"
<< "Enter 4 to delete a value"
<< "\n"
<< "Enter 5 to search value"
<< "\n"
<< "Enter 6 to reverse linked list"
<< "\n"
<< "Enter 7 to print your linked list"
<< "\n"
<< "Enter 0 to stop the Game"
<< "\n\n";
int choice = 1;
int value, pos;
cout << "Alert! initially insert 5-10 value then apply other operation\n\n";
while (choice <= 7 && choice > 0)
{
switch (choice)
{
case 1:
cout << "Enter a value: ";
cin >> value;
addElement(head, value);
break;
case 2:
cout << "Enter a value and position: ";
cin >> value >> pos;
push(head, value, pos);
break;
case 3:
cout << "Enter a value and position: ";
cin >> value >> pos;
updateValue(head, value, pos);
break;
case 4:
cout << "Enter a value which you wanna delete: ";
cin >> value;
deleteValue(head, value);
break;
case 5:
cout << "Enter a value which you wanna search: ";
cin >> value;
storeArr arr;
arr = searchValue(head, value);
if (arr.position[0] == 1)
{
cout << "Searched value not found" << endl;
}
else
{
int size = arr.position[0];
cout << "The value is founded at: ";
for (int i = 1; i < size; i++)
{
cout << arr.position[i];
if (i < size - 1)
{
cout << ", ";
}
}
cout << endl;
}
break;
case 6:
reverse(head);
cout << "linked list is now reversed" << endl;
break;
case 7:
cout << "Your linked list: ";
print(head);
break;
default:
break;
}
cout << "Enter your choice: ";
cin >> choice;
}
cout << "Your final linked list:" << endl;
print(head);
return 0;
}