-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubly Linked List.cpp
286 lines (262 loc) · 6.83 KB
/
Doubly 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
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *next;
node *prev;
node(int val)
{
data = val;
next = NULL;
prev = NULL;
}
};
void insertAtHead(node *&head, int val)
{
node *n = new node(val);
n->next = head;
if (head != NULL)
head->prev = n;
head = n;
cout << "Added " << val << " to the Head of Linked List!\n";
}
void insertAtTail(node *&head, int val)
{
if (head == NULL)
{
cout << "Linked List Initialized! \n";
insertAtHead(head, val);
return;
}
node *n = new node(val);
node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = n;
n->prev = temp;
cout << "Added " << val << " to the Tail of Linked List!\n";
}
void displayLinkedList(node *head)
{
node *temp = head;
cout << "\nThe contents of the Linked List Are: \n";
while (temp != NULL)
{
cout << temp->data << " <--> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
void insertAfterN(node *&head, int val, int index)
{
int count = 0;
node *temp = head;
node *n = new node(val);
while (temp->next != NULL && count != index)
{
temp = temp->next;
count++;
}
if (temp->next == NULL && count != index)
{
cout << "Index out of Bound!\n";
delete n;
return;
}
temp->next->prev = n;
n->next = temp->next;
temp->next = n;
n->prev = temp;
cout << val << " inserted after index " << index << endl;
}
void insertBeforeN(node *&head, int val, int index)
{
int count = 0;
node *temp = head;
node *n = new node(val);
while (temp != NULL && count != index)
{
++count;
temp = temp->next;
}
if (temp == NULL && count != index)
{
cout << "Index out of Bound!\n";
delete n;
return;
}
temp->prev->next = n;
n->prev = temp->prev;
n->next = temp;
temp->prev = n;
cout << val << " inserted before index " << index << endl;
}
void deleteFromHead(node *&head)
{
if (head)
{
node *temp = head;
cout << "Deleted " << temp->data << " from Head!\n";
head = head->next;
head->prev = NULL;
delete temp;
}
else
cout << "List is Empty!\n";
}
void deleteFromTail(node *&head)
{
if (!head)
cout << "List is Empty!\n";
else
{
node *temp = head;
while (temp->next != NULL)
temp = temp->next;
cout << "Deleted " << temp->data << " from Tail!\n";
temp->prev->next = NULL;
delete temp;
}
}
void deleteAfter(node *&head, int val)
{
// traverse garna lai
node *temp = head;
while (temp->data != val && temp != NULL)
{
temp = temp->next;
}
if (temp == NULL)
{
cout << "Node not Found!\n";
return;
}
// bahira niskida samma tyo element ma aaisakeko hunxa jasko AFTER ko element lai hataune ho
if (temp->next != NULL)
{
if (temp->next->next == NULL)
{
deleteFromTail(head);
return;
}
cout << temp->next->data << " deleted!\n";
temp->next->next->prev = temp;
temp->next = temp->next->next;
}
else
{
cout << "No Element after " << temp->data << endl;
}
}
void deleteBefore(node *&head, int val)
{
// traverse garna lai
node *temp = head;
while (temp->data != val && temp != NULL)
{
temp = temp->next;
}
if (temp == NULL)
{
cout << "Node not Found!\n";
return;
}
// bahira niskida samma tyo element ma aaisakeko hunxa jasko BEFORE ko element lai hataune ho
if (temp->prev != NULL)
{
if (temp->prev->prev == NULL)
{
deleteFromHead(head);
return;
}
cout << temp->prev->data << " deleted!\n";
temp->prev->prev->next = temp;
temp->prev = temp->prev->prev;
}
else
{
cout << "No Element Before " << temp->data << endl;
}
}
void displayMenu()
{
cout << "1. INSERT NODE AT BEGINNING OF THE LIST\n"
<< "2. INSERT NODE AT END OF THE LIST\n"
<< "3. INSERT NODE AFTER SPECIFIC NODE\n"
<< "4. INSERT NODE BEFORE SPECIFIC NODE\n"
<< "5. DELETE NODE FROM BEGINNING OF THE LIST\n"
<< "6. DELETE NODE FROM END OF THE LIST\n"
<< "7. DELETE NODE AFTER SPECIFIC NODE\n"
<< "8. DELETE NODE BEFORE SPECIFIC NODE\n"
<< "9. DISPLAY THE LINKED LIST\n"
<< "ENTER 0 TO QUIT\n";
}
int main()
{
system("cls");
node *head = NULL;
int choice, value, index;
displayMenu();
do
{
cout << "Enter the operation you want to do: " << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter the Value that you want to insert at the beginning: ";
cin >> value;
insertAtHead(head, value);
break;
case 2:
cout << "Enter the Value that you want to insert at the end: ";
cin >> value;
insertAtTail(head, value);
break;
case 3:
cout << "Enter the Value that you want to insert after the node: ";
cin >> value;
cout << "Enter the index after which you want to insert the value: ";
cin >> index;
insertAfterN(head, value, index);
break;
case 4:
cout << "Enter the Value that you want to insert before th node: ";
cin >> value;
cout << "Enter the index after which you want to insert the value: ";
cin >> index;
insertBeforeN(head, value, index);
break;
case 5:
deleteFromHead(head);
break;
case 6:
deleteFromTail(head);
break;
case 7:
cout << "Enter the Value after which you want to delete the node: ";
cin >> value;
deleteAfter(head, value);
break;
case 8:
cout << "Enter the Value before which you want to delete the node: ";
cin >> value;
deleteBefore(head, value);
break;
case 9:
displayLinkedList(head);
break;
case 0:
cout << "Thanks For Using Linked List Implementation by Saksham078BCT072 :) \n";
break;
default:
cout << "Enter Valid Choice! " << endl;
break;
}
} while (choice != 0);
return 0;
}