-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeque.cpp
313 lines (312 loc) · 4.95 KB
/
Deque.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
#include <iostream>
#include <iomanip>
using namespace std;
template <class t>
class node
{
public:
t info;
node<t> *next, *prev;
node()
{
next = 0;
prev = 0;
}
node(t x, node<t> *n = 0, node<t> *p = 0)
{
info = x;
next = n;
prev = p;
}
};
template <class t>
class deque
{
node<t>* front;
node<t>* rear;
int size;
public:
deque()
{
front = rear = 0;
}
void insertfront(t x);
void insertback(t x);
t removefront();
t removeback();
int isempty();
int count();
void display();
void clear();
t frontmost();
t rearmost();
};
template <class t>
int deque<t>::isempty()
{
if (front == 0)
return 1;
else
return 0;
}
template <class t>
void deque<t>::insertfront(t x)
{
node<t> *temp;
temp = new node<t> (x);
if (isempty())
{
front = rear = temp;
}
else
{
temp -> next = front;
front -> prev = temp;
front = temp;
}
}
template <class t>
void deque<t>::insertback(t x)
{
node<t> *temp;
temp = new node<t> (x);
if (isempty())
{
front = rear = temp;
}
else
{
rear -> next = temp;
temp -> prev = rear;
rear = temp;
}
}
template<class t>
int deque<t>::count()
{
int count = 0;
node<t> *temp;
for (temp = front; temp != NULL; temp = temp->next)
count++;
return count;
}
template <class t>
t deque<t>::removefront()
{
t x;
if (front == rear)
{
x = front -> info;
delete front;
front = rear = 0;
}
else
{
x = front -> info;
front = front -> next;
delete (front -> prev);
front -> prev = 0;
}
return x;
}
template <class t>
t deque<t>::removeback()
{
t x;
if (front == rear)
{
x = rear -> info;
delete rear;
front = rear = 0;
}
else
{
x = rear -> info;
rear = rear -> prev;
delete (rear -> next);
rear -> next = 0;
}
return x;
}
template <class t>
void deque<t>::display()
{
node<t> *temp;
temp = front;
cout << "\nContents of Double Ended queue List from head to tail are!!! : ";
while (temp != 0)
{
cout << setw(5) << temp -> info;
temp = temp -> next;
}
}
template <class t>
t deque<t>::frontmost()
{
t fm;
fm = front->info;
return fm;
}
template <class t>
t deque<t>::rearmost()
{
t rm;
rm = rear->info;
return rm;
}
template<class t>
void deque<t>::clear()
{
node<t> *temp;
/* temp=front->next;
do
{
temp=temp->next;
}while(temp!=front->next);
front=rear=0;
*/
rear = NULL;
while (front != NULL)
{
temp = front;
front = front->next;
delete(temp);
}
size = 0;
}
int main()
{
deque<int> l1;
int choice, el, d, e;
char ch = 'Y';
cout << "*-------------MENU-------------*\n";
cout << "1. Insert a node at Beginning\n";
cout << "2. Insert a node at End\n";
cout << "3. Delete from Beginning\n";
cout << "4. Delete from End\n";
cout << "5. Count the number of elements\n";
cout << "6. Display Frontmost element\n";
cout << "7. Display Rearmost element\n";
cout << "8. Check for the emptyness of queue\n";
cout << "9. Clear the list\n";
while (ch == 'y' || ch == 'Y')
{
cout << "Enter choice: ";
cin >> choice;
switch (choice)
{
case 1 :
{
cout << "Enter the element you want to insert in beginning: ";
cin >> el;
l1.insertfront(el);
l1.display();
break;
}
case 2:
{
cout << "Enter the element you want to insert in end: ";
cin >> el;
l1.insertback(el);
l1.display();
break;
}
case 3:
{
d = l1.isempty();
if (d == 1)
{
cout << "\nSorry but the queue is empty :( ";
}
else
{
l1.removefront();
l1.display();
}
break;
}
case 4:
{
d = l1.isempty();
if (d == 1)
{
cout << "\nSorry but the queue is empty :( ";
}
else
{
l1.removeback();
l1.display();
}
break;
}
case 5:
{
d = l1.isempty();
if (d == 1)
{
cout << "\nSorry but the queue is empty :( ";
}
else
{
e = l1.count();
cout << "\nThe total number of elements are : " << e << endl;
l1.display();
}
break;
}
case 6:
{
d = l1.isempty();
if (d == 1)
{
cout << "\nSorry but the queue is empty :( ";
}
else
{
e = l1.frontmost();
cout << "\nThe frontmost element is : " << e << endl;
l1.display();
}
break;
}
case 7:
{
d = l1.isempty();
if (d == 1)
{
cout << "\nSorry but the queue is empty :( ";
}
else
{
e = l1.rearmost();
cout << "\nThe rearmost element is : " << e << endl;
l1.display();
}
break;
}
case 8:
{
d = l1.isempty();
if (d == 1)
{
cout << "\n Queue is empty!";
}
else
{
cout << "\n Queue is not empty!!";
}
break;
}
case 9:
{
l1.clear();
cout << "\nThe Queue is cleared !" << endl;
break;
}
default:
cout << "\nInput Invalid :( ";
}
cout << "\nDo you want to continue ?(y/n)";
cin >> ch;
}
}