-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdouble_linked_list.cpp
More file actions
381 lines (346 loc) · 9.71 KB
/
double_linked_list.cpp
File metadata and controls
381 lines (346 loc) · 9.71 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
#include<iostream>
using namespace std;
template <class T>
class Node
{
public:
T data;
Node<T> *next;
Node<T> *prev;
Node()
{
next=NULL;
prev=NULL;
}
};
template <class T>
class DLLlinkedlist
{
Node<T>* head;
Node<T>* tail;
public:
DLLlinkedlist()
{
head=NULL;
tail=NULL;
}
void add_to_head(T d)
{
Node<T>* new_node=new Node<T>();
new_node->data=d;
new_node->prev=NULL;
if(head==NULL)
{
head=new_node;
tail=new_node;
}
else
{
head->prev=new_node;
new_node->next=head;
head=new_node;
}
}
void add_to_tail(T d)
{
Node<T>* new_node1=new Node<T>();
new_node1->data=d;
new_node1->next=NULL;
if(head==NULL)
{
head=new_node1;
tail=new_node1;
}
else
{
new_node1->prev=tail;
tail->next=new_node1;
tail=tail->next;
}
}
void add_to_nth(T d,int n)
{
Node<T>* new_node=new Node<T>();
new_node->data=d;
new_node->next=NULL;
if(head==NULL)
{
head=new_node;
tail=new_node;
}
else
{
int i=1;
Node<T>* temp=head;
while (temp!=NULL && i<n)
{
temp=temp->next;
i++;
}
if(temp->next==NULL)
{
new_node->data=d;
new_node->next=NULL;
new_node->prev=tail;
tail->next=new_node;
tail=tail->next;
}
else
{
temp->next->prev=new_node;
new_node->next=temp->next;
temp->next=new_node;
new_node->prev=temp;
}
}
}
void traverse()
{
if(head==NULL)
{
cout<<"List Is Empty.\n";
}
else
{
Node<T>* temp=head;
cout<<"The Linked list is :\n";
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
}
void reverse_traverse()
{
if(head==NULL)
{
cout<<"List Is Empty.\n";
}
else
{
Node<T>* temp=tail;
cout<<"The Linked list is :\n";
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->prev;
}
}
}
void del_from_head()
{
if(head==NULL)
{
cout<<"List is empty";
return;
}
else if(head==tail)
{
delete head;
head=NULL;
tail=NULL;
}
else
{
Node<T>* temp;
temp=head;
head->next->prev=NULL;
head=head->next;
delete temp;
}
}
void del_from_tail()
{
if (head==NULL)
{
cout<<"List is empty";
return;
}
else if (head==tail)
{
delete head;
head=NULL;
tail=NULL;
}
else
{
Node<T>* tem;
tem=head;
while(tem!=NULL)
{
if(tem->next==tail)
{
delete(tail);
tail=tem;
tem->next=NULL;
break;
}
tem=tem->next;
}
}
}
bool search(T n)
{
Node<T> *temp;
temp=head;
while(temp!=NULL)
{
if(temp->info == element)
return true;
temp = temp->next;
}
return false;
}
void ispalindrome()
{
Node<T>* temp=head;
Node<T>* temp1=tail;
int count=0;
while (temp!=NULL && temp1!=NULL)
{
if(temp->data==temp1->data)
{
temp=temp->next;
temp1=temp1->prev;
count=1;
}
else
{
count=0;
break;
}
}
if(count==1)
{
cout<<"\nis palindrome";
}
else
{
cout<<"\nnot a palindrome";
}
}
void del_after_Nth(int n)
{
int i=1;
Node<T>* temp;
temp=head;
if (temp==NULL)
{
cout<<"List is empty";
return;
}
else
{
while(temp->next!=NULL && i<n-1)
{
temp=temp->next;
i++;
}
if(temp->next==NULL)
{
delete(tail);
temp->next=NULL;
tail=temp;
}
else if (temp->next==tail)
{
delete(tail);
temp->next=NULL;
tail=temp;
}
else
{
temp->next->next->prev=temp;
temp->next=temp->next->next;
}
}
}
};
int main()
{
int n,q;
int e;
char a='y';
DLLlinkedlist<int> list;
while(1)
{
if(a=='y')
{
cout<<"1. Add the element at the head\n"
<<"2. Add the element at the tail\n"
<<"3. Delete the element from the head\n"
<<"4. Delete the element from the tail\n"
<<"5. Search the element in the linked list\n"
<<"6. Traverse the linked list\n"
<<"7. Add the element at Nth position\n"
<<"8. Print reverse linked list\n"
<<"9. Check is palindrome\n"
<<"10. Delete a node after a Nth position\n";
cout<<"\n\nEnter your choice: ";
cin>>q;
switch (q)
{
case 1:
cout<<"Enter the element you want to add: ";
cin>>e;
list.add_to_head(e);
list.traverse();
break;
case 2:
cout<<"Enter the element you want to add: ";
cin>>e;
list.add_to_tail(e);
list.traverse();
break;
case 3:
list.del_from_head();
list.traverse();
break;
case 4:
list.del_from_tail();
list.traverse();
break;
case 5:
cout<<"\nEnter the element to be searched ";
cin>>n;
cout<<endl;
if(list.search(n)==1)
{
cout<<"\nElement is Present in List\n";
}
else
cout<<"\nElement is not present in the List\n";
break;
case 6:
list.traverse();
break;
case 7:
cout<<"Enter the position to which you want to add: ";
cin>>n;
cout<<"Enter the element you want to add: ";
cin>>e;
list.add_to_nth(e,n);
list.traverse();
break;
case 8:
list.reverse_traverse();
break;
case 9:
list.ispalindrome();
break;
case 10:
cout<<"Enter the position after which you want to delete: ";
cin>>n;
list.del_after_Nth(n);
list.traverse();
}
cout<<"\n\nDo you want to continue if yes press 'y' ,if no press 'n': ";
cin>>a;
}
else
{
break;
}
}
}