-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathDoublyLinkedList.cpp
325 lines (324 loc) · 5.98 KB
/
DoublyLinkedList.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
// C++ program for the all Doubly Linked List Operation
// It Includes the following functions
// insert at Begininng
// insert at End
// insert at Position
// Delete at Beginning
// Delete at End
// Delete at Position
// Printing the Linked List
// Reverse The linked list
#include <bits/stdc++.h>
using namespace std;
// Node of the Linked List
class node
{
public:
node *prev;
int data;
node *next;
node(int value)
{
prev = NULL; // By default previous pointer is pointed to NULL
data = value; // value is assigned to the data
next = NULL; // By default next pointer is pointed to NULL
}
};
void insert_at_head(node *&head, int value)
{
// created new Node
node *n = new node(value);
// Arranging the pointers
n->next = head;
// If not a empty Linked List
if (head != NULL)
{
head->prev = n;
}
// Final Assignment
head = n;
}
void insert_at_tail(node *&head, int value)
{
// Same as insert_at_situation
if (head == NULL)
{
insert_at_head(head, value);
return;
}
// created new Node
node *n = new node(value);
// Temporary Point pointing towards the head
node *temp = head;
// Travelling to the last node
while (temp->next != NULL)
{
temp = temp->next;
}
// Pointers Arrangement
temp->next = n;
n->prev = temp;
}
// consider 1-based indexing while deciding Position
void insert_at_pos(node *&head, int pos, int value)
{
// If Linked List is Empty
if (head == NULL)
{
cout << "Linked List is Empty" << endl;
return;
}
// insert_at_head situation when pos==1
if (pos == 1)
{
insert_at_head(head, value);
return;
}
else
{
pos = pos - 2;
node *temp = head;
while (pos && temp != NULL)
{
temp = temp->next;
pos--;
}
// it means we have travelled the whole linked list but not able to find a place a valid position
if (pos > 0)
{
cout << "Invalid Position" << endl;
return;
}
// it means the insert_at_tail situation
else if (temp == NULL && pos == 0)
{
insert_at_tail(head, value);
return;
}
else
{
// stroing the address of the next_node
node *temp_next = temp->next;
// edge case node_before null
if (temp_next == NULL)
{
// New Node Allocation
node *n = new node(value);
// Arranging Pointers
temp->next = n;
n->prev = temp;
n->next = NULL;
return;
}
// creating new node
node *n = new node(value);
// linking all the pointers
temp->next = n;
n->prev = temp;
n->next = temp_next;
temp_next->prev = n;
}
}
}
void delete_at_head(node *&head)
{
// If Linked List is Empty
if (head == NULL)
{
cout << "Linked List is Empty" << endl;
return;
}
node *temp = head;
temp = temp->next;
temp->prev = NULL;
head = temp;
}
void delete_at_end(node *&head)
{
// If Linked List is Empty
if (head == NULL)
{
cout << "Linked List is Empty" << endl;
return;
}
node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
// if only single node is present
if (temp->prev == NULL)
{
head = NULL;
return;
}
else
{
temp = temp->prev;
temp->next = NULL;
}
}
void delete_at_pos(node *&head, int pos)
{
// If Linked List is Empty
if (head == NULL)
{
cout << "Linked List is Empty" << endl;
return;
}
// delete_at_head situation
if (pos == 1)
{
delete_at_head(head);
return;
}
// All other cases
node *temp = head;
pos--;
while (pos && temp != NULL)
{
temp = temp->next;
pos--;
}
// Invalid Position Case
if (temp == NULL)
{
cout << "Invalid Position" << endl;
return;
}
// if we are at last node
else if (temp->next == NULL)
{
temp = temp->prev;
temp->next = NULL;
return;
}
// when a middle case
else
{
node *temp_next = temp->next;
node *temp_prev = temp->prev;
temp_prev->next = temp_next;
temp_next->prev = temp_next;
return;
}
}
void reverse(node *&head)
{
// If Linked List is Empty
if (head == NULL)
{
cout << "Empty Linked List" << endl;
}
// Using stack for reversing
// It has a LIFO structure (Last In First Out)
stack<int> st;
node *temp = head;
while (temp != NULL)
{
st.push(temp->data);
temp = temp->next;
}
// added all the elements sequence wise in the
// stack
temp = head;
while (temp != NULL)
{
temp->data = st.top();
st.pop();
temp = temp->next;
}
// popped all the elements and the added in the
// linked list,
// which are in the reversed order finally
}
// printing the Linked List
void display(node *head)
{
node *temp = head;
while (temp != NULL)
{
cout << temp->data << " --> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
// Driver code
int main()
{
node *head = NULL;
cout << "Welcome to the Doubly Linked List Program" << endl;
while (true)
{
cout << "Press 1 To Display" << endl;
cout << "Press 2 To Insert At Head" << endl;
cout << "Press 3 To Insert At Tail" << endl;
cout << "Press 4 To Insert At Position" << endl;
cout << "Press 5 To Delete At Head" << endl;
cout << "Press 6 To Delete At End" << endl;
cout << "Press 7 To Delete At Position" << endl;
cout << "Press 8 To Reverse The List" << endl;
cout << "Press 9 To Exit" << endl;
int ch;
cout << "Enter Your Choice :";
cin >> ch;
if (ch == 1)
{
cout << "Linked List : ";
display(head);
}
else if (ch == 2)
{
int value;
cout << "Enter the value :";
cin >> value;
insert_at_head(head, value);
}
else if (ch == 3)
{
int value;
cout << "Enter the value :";
cin >> value;
insert_at_tail(head, value);
}
else if (ch == 4)
{
int value;
cout << "Enter the value :";
cin >> value;
int position;
cout << "Enter the position :";
cin >> position;
insert_at_pos(head, position, value);
}
else if (ch == 5)
{
delete_at_head(head);
}
else if (ch == 6)
{
delete_at_end(head);
}
else if (ch == 7)
{
int position;
cout << "Enter the position :";
cin >> position;
delete_at_pos(head, position);
}
else if (ch == 8)
{
reverse(head);
}
else if (ch == 9)
{
cout << "Thanks For Using..Hope You Enjoyed it" << endl;
break;
}
else
{
cout << "Wrong Choice" << endl;
}
}
return 0;
}