-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDequeue.cpp
169 lines (152 loc) · 3.06 KB
/
Dequeue.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
#include<iostream>
using namespace std;
#define SIZE 5
class dequeue
{
int a[10], front, rear, count;
public:
dequeue();
void add_at_beg(int);
void add_at_end(int);
void delete_fr_front();
void delete_fr_rear();
void display();
};
dequeue::dequeue()
{
front = -1;
rear = -1;
count = 0;
}
void dequeue::add_at_beg(int item)
{
int i;
if (front == -1)
{
front++;
rear++;
a[rear] = item;
count++;
}
else if (rear >= SIZE - 1)
{
cout << "\nInsertion is not possible, overflow!!!!";
}
else
{
for (i = count; i >= 0; i--)
{
a[i] = a[i - 1];
}
a[i] = item;
count++;
rear++;
}
}
void dequeue::add_at_end(int item)
{
if (front == -1)
{
front++;
rear++;
a[rear] = item;
count++;
}
else if (rear >= SIZE - 1)
{
cout << "\nInsertion is not possible, overflow!!!";
return;
}
else
{
a[++rear] = item;
}
}
void dequeue::display()
{
for (int i = front; i <= rear; i++)
{
cout << a[i] << " ";
}
}
void dequeue::delete_fr_front()
{
if (front == -1)
{
cout << "Deletion is not possible: Dequeue is empty";
return;
}
else
{
if (front == rear)
{
front = rear = -1;
return;
}
cout << "The deleted element is " << a[front];
front = front + 1;
}
}
void dequeue::delete_fr_rear()
{
if (front == -1)
{
cout << "Deletion is not possible: Dequeue is empty";
return;
}
else
{
if (front == rear)
{
front = rear = -1;
}
cout << "The deleted element is " << a[rear];
rear = rear - 1;
}
}
int main()
{
int c, item;
dequeue d1;
do
{
cout << "\n\n****DEQUEUE OPERATION****\n";
cout << "\n1-Insert at beginning";
cout << "\n2-Insert at end";
cout << "\n3-Display";
cout << "\n4-Deletion from front";
cout << "\n5-Deletion from rear";
cout << "\n6-Exit";
cout << "\nEnter your choice <1-6>: ";
cin >> c;
switch (c)
{
case 1:
cout << "Enter the element to be inserted: ";
cin >> item;
d1.add_at_beg(item);
break;
case 2:
cout << "Enter the element to be inserted: ";
cin >> item;
d1.add_at_end(item);
break;
case 3:
d1.display();
break;
case 4:
d1.delete_fr_front();
break;
case 5:
d1.delete_fr_rear();
break;
case 6:
exit(0);
break;
default:
cout << "Invalid choice";
break;
}
} while (c != 6);
return 0;
}