forked from riya2001-cloud/java-hacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular-doubly-linked-list.cpp
306 lines (282 loc) · 8.07 KB
/
circular-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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Kashish Ahuja
// Program for operations on circular doubly linked list
#include<iostream>
using namespace std;
#include<stdlib.h>
struct CircularDoublyLinkedList
{
int data;
struct CircularDoublyLinkedList *prev, *next;
};
typedef struct CircularDoublyLinkedList node;
node *start=NULL;
node *last=NULL;
class CDLL
{
private:
int count = 0;
public:
void insertAtBegin()
{
node *p;
p=new node;
cout<<"\nEnter element to be inserted: ";
cin>>p->data;
if(start==NULL)
{
start=last=p;
p->prev=p->next=p;
}
else
{
p->next=start;
start->prev=p;
start=p;
start->prev=last;
last->next=start;
}
cout<<p->data<<" is inserted at the beginning";
count++;
}
void insertAtPosition()
{
int pos;
cout<<"\nEnter position to insert element: ";
cin>>pos;
if(pos>0 && pos<=count+1)
{
node *p;
p=new node;
cout<<"Enter element to be inserted: ";
cin>>p->data;
if(pos==1)
{
if(start==NULL)
{
start=last=p;
p->prev=p->next=p;
}
else
{
p->next=start;
start->prev=p;
start=p;
start->prev=last;
last->next=start;
}
}
else if(pos==count+1)
{
last->next=p;
p->prev=last;
last=p;
last->next=start;
start->prev=last;
}
else
{
node *temp;
temp=start;
int i=1;
while(i<pos-1)
{
temp=temp->next;
i++;
}
p->next=temp->next;
p->prev=temp;
temp->next->prev=p;
temp->next=p;
}
cout<<p->data<<" is inserted at position "<<pos;
count++;
}
else
cout<<"\nPosition "<<pos<<" does not exist in list";
}
void insertAtEnd()
{
node *p;
p=new node;
cout<<"\nEnter element to be inserted: ";
cin>>p->data;
if(start==NULL)
{
start=last=p;
p->prev=p->next=p;
}
else
{
last->next=p;
p->prev=last;
last=p;
last->next=start;
start->prev=last;
}
cout<<p->data<<" is inserted at the end";
count++;
}
void displayForward()
{
node *temp;
temp=start;
cout<<"\nList: ";
if(start!=NULL)
{
while(temp->next != start)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<temp->data;
}
}
void displayBackward()
{
node *temp;
temp=last;
cout<<"\nList: ";
if(last!=NULL)
{
while(temp->prev!=last)
{
cout<<temp->data<<" ";
temp=temp->prev;
}
cout<<temp->data<<" ";
}
}
void deleteFromBegin()
{
if(start==NULL)
cout<<"\nList underflow";
else
{
node *temp;
temp=start;
if(start==last)
start=last=NULL;
else
{
start=start->next;
start->prev=last;
last->next=start;
}
cout<<"\n"<<temp->data<<" is deleted from beginning";
delete(temp);
count--;
}
}
void deleteFromPosition()
{
if(start==NULL)
cout<<"\nList underflow";
else
{
int pos;
cout<<"\nEnter position to delete element: ";
cin>>pos;
if(pos>0 && pos<=count)
{
node *temp;
if(pos==1)
{
temp=start;
if(start==last)
start=last=NULL;
else
{
start=start->next;
start->prev=last;
last->next=start;
}
}
else if(pos==count)
{
temp=last;
last=last->prev;
last->next=start;
start->prev=last;
}
else
{
node *p, *q;
temp=start;
int i=1;
while(i<pos)
{
p=temp;
temp=temp->next;
i++;
}
p->next=temp->next;
q=temp->next;
q->prev=p;
temp->next=temp->prev=NULL;
}
cout<<temp->data<<" is deleted from position "<< pos;
delete(temp);
count--;
}
else
cout<<"Position "<<pos<<" does not exist in list";
}
}
void deleteFromEnd()
{
if(start==NULL)
cout<<"\nList underflow";
else
{
node *temp;
if(start==last)
{
temp=start;
start=last=NULL;
}
else
{
temp=last;
last=last->prev;
last->next=start;
start->prev=last;
}
cout<<"\n"<<temp->data<<" is deleted from end";
delete(temp);
count--;
}
}
};
int main()
{
CDLL cdll;
int choice;
do
{
cout<<"\n\n1) Insert an element at the beginning\n2) Insert an element at a position\n3) Insert an element at the end\n4) Display (Forward)\n5) Display (Backward)\n6) Delete an element from the beginning\n7) Delete an element from a position\n8) Delete an element from the end\n9) Exit";
cout<<"\nEnter you choice: ";
cin>>choice;
switch (choice)
{
case 1: cdll.insertAtBegin();
break;
case 2: cdll.insertAtPosition();
break;
case 3: cdll.insertAtEnd();
break;
case 4: cdll.displayForward();
break;
case 5: cdll.displayBackward();
break;
case 6: cdll.deleteFromBegin();
break;
case 7: cdll.deleteFromPosition();
break;
case 8: cdll.deleteFromEnd();
break;
case 9: exit(0);
default: cout<<"\nInvalid choice";
}
} while (choice!=9);
return 0;
}