-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
339 lines (319 loc) · 8.97 KB
/
main.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<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct nod {
int info;
struct nod *n;
struct nod *p;
}*start, *last;
int count = 0;
class circulardoublylist {
public:
nod *create_node(int);
void create_node_success();
void display();
void insert_begin();
void insert_end();
void insert_position();
void delete_begin();
void delete_end();
void delete_position();
circulardoublylist() {
start = NULL;
last = NULL;
}
};
int main() {
int c;
circulardoublylist cdll;
while (1)
{
cout<<"***** MAIN MENU *****"<<endl;
cout<<"1: Create a list"<<endl;
cout<<"2: Display the list"<<endl;
cout<<"3: Add a node at the begining"<<endl;
cout<<"4: Add a node at the end"<<endl;
cout<<"5: Add a node before a given node"<<endl;
cout<<"6: Delete a node from the begining"<<endl;
cout<<"7: Delete a node from the end"<<endl;
cout<<"8: Delete a given node"<<endl;
cout<<"9: Exit"<<endl;
cout<<"Enter your option : ";
cin>>c;
switch(c) {
case 1:
//List creation messege
cdll.create_node_success();
break;
case 2:
//display list
cdll.display();
break;
case 3:
//insert at begin
cdll.insert_begin();
break;
case 4:
//insert at end
cdll.insert_end();
break;
case 5:
//insert at position
cdll.insert_position();
break;
case 6:
//delete begin
cdll.delete_begin();
break;
case 7:
// delete end
cdll.delete_end();
break;
case 8:
//delete at position
cdll.delete_position();
break;
case 9:
//successful termination
exit(1);
default:
cout<<"!!!!!!!!!!!!!"<<endl;
cout<<"Wrong choice"<<endl;
cout<<"!!!!!!!!!!!!!"<<endl;
}
}
return 0;
}
//Create list
//Menu No 1
nod* circulardoublylist::create_node(int v) {
count++;
struct nod *t;
t = new(struct nod);
t->info = v;
t->n = NULL;
t->p = NULL;
return t;
}
void circulardoublylist::create_node_success() {
cout<<"-----------------------------------"<<endl;
cout<<"\n Success : List created.\n"<<endl;
cout<<"-----------------------------------"<<endl;
}
//display list
//Menu No 2
void circulardoublylist::display() {
int i;
struct nod *s;
if (start == last && start == NULL) {
cout<<"-----------------------------------"<<endl;
cout<<"The List is empty, nothing to display"<<endl;
cout<<"-----------------------------------"<<endl;
return;
}
s = start;
cout<<"--------------------------------------"<<endl;
for (i = 0;i < count-1;i++) {
cout<<s->info<<"<->";
s = s->n;
}
cout<<s->info<<endl;
cout<<"--------------------------------------"<<endl;
}
//insert at begin
//Menu No 3
void circulardoublylist::insert_begin() {
int v;
cout<<"----------------------------------------------"<<endl;
cout<<endl<<"Enter the element to be inserted: ";
cin>>v;
cout<<"----------------------------------------------"<<endl;
struct nod *t;
t = create_node(v);
if (start == last && start == NULL) {
cout<<"-----------------------------------------------------------"<<endl;
cout<<"Success : Element inserted in empty list at Begin position"<<endl;
cout<<"----------------------------------------------------------"<<endl;
start = last = t;
start->n = last->n = NULL;
start->p = last->p = NULL;
} else {
t->n = start;
start->p = t;
start = t;
start->p = last;
last->n = start;
cout<<"----------------------------------------------"<<endl;
cout<<"Success : Element inserted at Begin position."<<endl;
cout<<"----------------------------------------------"<<endl;
}
}
//Insert at end
//Menu No 4
void circulardoublylist::insert_end() {
int v;
cout<<"--------------------------------------------"<<endl;
cout<<endl<<"Enter the element to be inserted: ";
cin>>v;
cout<<"--------------------------------------------"<<endl;
struct nod *t;
t = create_node(v);
if (start == last && start == NULL) {
cout<<"-----------------------------------------------"<<endl;
cout<<"Element inserted in empty list at End position"<<endl;
cout<<"----------------------------------------------"<<endl;
start = last = t;
start->n= last->n = NULL;
start->p = last->p= NULL;
} else {
last->n= t;
t->p= last;
last = t;
start->p = last;
last->n= start;
}
}
//Insert at node position
//Menu No 5
void circulardoublylist::insert_position() {
int v, pos, i;
cout<<"---------------------------------------------------------------"<<endl;
cout<<endl<<"Enter the element to be inserted: ";
cin>>v;
cout<<endl<<"Enter the node position of element to be inserted: ";
cout<<"---------------------------------------------------------------"<<endl;
cin>>pos;
struct nod *t, *s, *ptr;
t = create_node(v);
if (start == last && start == NULL) {
if (pos == 1) {
start = last = t;
start->n = last->n = NULL;
start->p = last->p = NULL;
} else {
cout<<"------------------------"<<endl;
cout<<"Position out of range"<<endl;
cout<<"------------------------"<<endl;
count--;
return;
}
} else {
if (count < pos) {
cout<<"------------------------"<<endl;
cout<<"Position out of range"<<endl;
cout<<"------------------------"<<endl;
count--;
return;
}
s = start;
for (i = 1;i <= count;i++) {
ptr = s;
s = s->n;
if (i == pos - 1) {
ptr->n = t;
t->p= ptr;
t->n= s;
s->p = t;
cout<<"---------------------------------------------"<<endl;
cout<<"Success : Element inserted at given position."<<endl;
cout<<"---------------------------------------------"<<endl;
break;
}
}
}
}
//Delete at begin
//Menu No 6
void circulardoublylist::delete_begin() {
int pos, i;
nod *ptr, *s;
s = start;
if (start == last && start == NULL) {
cout<<"------------------------------------"<<endl;
cout<<"List is empty, nothing to delete"<<endl;
cout<<"------------------------------------"<<endl;
}
else{
count--;
last->n = s->n;
s->n->p = last;
start = s->n;
free(s);
cout<<"-----------------------------------"<<endl;
cout<<"Warning! : Element Deleted at Begin!"<<endl;
cout<<"-----------------------------------"<<endl;
return;
}
}
//Delete at end
//Menu No 7
void circulardoublylist::delete_end() {
int pos, i;
nod *ptr, *l;
l = last;
if (start == last && start == NULL) {
cout<<"-----------------------------------"<<endl;
cout<<"List is empty, nothing to delete"<<endl;
cout<<"-----------------------------------"<<endl;
}
else{
count--;
last->n = l->n;
l->n->p = last;
start = l->n;
free(l);
cout<<"-----------------------------------"<<endl;
cout<<"Warning! : Element Deleted at End!"<<endl;
cout<<"-----------------------------------"<<endl;
return;
}
}
//Delete at node position
//Menu No 8
void circulardoublylist::delete_position() {
int pos, i;
nod *ptr, *s;
if (start == last && start == NULL) {
cout<<"-----------------------------------"<<endl;
cout<<"List is empty, nothing to delete"<<endl;
cout<<"-----------------------------------"<<endl;
return;
}
cout<<"-------------------------------------------------------------"<<endl;
cout<<endl<<"Enter the node position of element to be deleted: ";
cin>>pos;
cout<<"-------------------------------------------------------------"<<endl;
if (count < pos) {
cout<<"------------------------"<<endl;
cout<<"Position out of range"<<endl;
cout<<"------------------------"<<endl;
return;
}
s = start;
if(pos == 1) {
count--;
last->n = s->n;
s->n->p = last;
start = s->n;
free(s);
cout<<"----------------------------------------------"<<endl;
cout<<"Warning! : Element Deleted at given position!"<<endl;
cout<<"----------------------------------------------"<<endl;
return;
}
for (i = 0;i < pos - 1;i++ ) {
s = s->n;
ptr = s->p;
}
ptr->n = s->n;
s->n->p = ptr;
if (pos == count) {
last = ptr;
}
count--;
free(s);
cout<<"----------------------------------------------"<<endl;
cout<<"Warning! : Element Deleted at given position!"<<endl;
cout<<"----------------------------------------------"<<endl;
}