-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathstack_queue.cpp
259 lines (234 loc) · 3.88 KB
/
stack_queue.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
#include <iostream>
#include <cstdlib>
using namespace std;
#define s_size 20
#define q_size 20
class stack
{
int top;
int *a;
public:
stack();
int push(int);
int pop();
void display();
int is_empty();
int is_full();
};
class queue
{
int front;
int rear;
int *b;
public:
queue();
int enqueue(int);
int dequeue();
void display_queue();
int isfull();
int isempty();
};
stack :: stack()
{
top = -1;
a = new int[s_size];
}
int stack ::is_empty()
{
if (top == -1)
{
return 1;
}
else
{
return 0;
}
}
int stack ::is_full()
{
if (top == s_size - 1)
{
return 1;
}
else
{
return 0;
}
}
int stack ::push(int d)
{
if (is_full())
{
cout << "Stack Overflow..!!" << endl;
return 0;
}
else
{
top++;
a[top] = d;
}
}
int stack ::pop()
{
int d;
if (is_empty())
{
cout << "Stack is Empty,cannot pop the elements." << endl;
return 0;
}
else
{
d = a[top];
top--;
return d;
}
}
void stack ::display()
{
if (is_empty() == 1)
{
cout << "Stack is Empty..!!" << endl;
}
else
{
for (int i = top; i >= 0; i--)
{
cout << a[i] << endl;
}
}
cout << endl;
}
queue ::queue()
{
front = -1;
rear = -1;
b = new int[q_size];
}
int queue ::isempty()
{
if ((front == -1 && rear == -1) || front > rear)
{
return 1;
}
else
{
return 0;
}
}
int queue::isfull()
{
if (rear == q_size - 1)
{
return 1;
}
else
{
return 0;
}
}
int queue ::enqueue(int data)
{
if (isempty() == 1)
{
front = 0;
rear++;
b[rear] = data;
return data;
}
else if (rear != - 1)
{
++rear;
b[rear] = data;
return data;
}
else
{
cout << "Queue is full" << endl;
return 0;
}
}
int queue ::dequeue()
{
if (isempty() == 1)
{
cout << "Queue is Empty ,Nothing to Dequeue" << endl;
return 0;
}
else if (front == rear)
{
int p = b[front];
front = -1;
rear = -1;
return p;
}
else
{
int p = b[front];
front++;
return p;
}
}
void queue ::display_queue()
{
if (isempty() == 1)
{
cout << "Queue is empty,nothing to display" << endl;
}
else
{
for (int i = front; i <= rear; i++)
{
cout << b[i] << " ";
}
}
cout << endl;
}
int main()
{
int choice, data, data_store;
char ch;
stack s;
queue t;
cout << endl;
cout << "1.Display Stack \n2.Push\n3.pop\n\n4.Display Queue\n5.enqueue\n6.dequeue\n7.exit\n"
<< endl;
cout << endl;
while (1)
{
cout << "Enter Your Choice : ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
s.display();
break;
case 2:
cout << "Enter the data you want to add :" << endl;
cin >> data;
s.push(data);
break;
case 3:
data_store = s.pop();
cout << data_store << " "<< "is Successfully removed " << endl;
break;
case 4:
t.display_queue();
break;
case 5:
cout << "Enter the data you want to add :" << endl;
cin >> data;
data_store =t.enqueue(data);
break;
case 6:
data_store = t.dequeue();
cout << data_store << " " << "is Successfully removed " << endl;
break;
case 7:
exit(0);
default:
cout << "Enter a valid Choice !!!!!!" << endl;
break;
}
}
}