-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
two_stacks_using_single_array.cpp
274 lines (248 loc) · 5.49 KB
/
two_stacks_using_single_array.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
/*Problem Statement- Cpp program to implement Two stacks using a single array. The operations push and pop is to be performed on two stacks and this should be done in one array.
Approach- Top of first stack will start from index 0 of the array and move towards right of the array as more elements are pushed into stack 1. Top of second stack will start from the end of the array(size-1) and stack 2 will grow in left direction when elements are pushed into it.
when both the stacks meet each other(top of first stack = top of second stack-1) we cannot push any element in both stacks.
*/
#include <iostream>
#define size 20
using namespace std;
int array[size];
int top1 = -1;
int top2 = size;
/*Function to push element in stack 1*/
void push_in_stack1(int data){
if(top1 < top2-1){
array[++top1] = data;
cout<<data<<" pushed onto stack 1"<<endl;
}
/*when the value of top1 = top2-1, the array is completely used up and we cannot push more elements in it.*/
else{
cout<<"Stack Overflow!"<<endl;
}
}
/*Function to push element in stack 2*/
void push_in_stack2(int data){
if(top1 < top2-1){
array[--top2] = data;
cout<<data<<" pushed onto stack 2"<<endl;
}
else{
cout<<"Stack Overflow!"<<endl;
}
}
/*Function to pop an element in stack 1*/
void pop_from_stack1(){
if(top1 > -1){
int popped_data = array[top1];
top1--;
cout<<popped_data<<" popped from stack 1"<<endl;
}
/*when the value of top is -1, there is no element present in the stack, hence pop operation cannot be performed.*/
else{
cout<<"Stack 1 is Empty!"<<endl;
}
}
/*Function to pop an element in stack 2*/
void pop_from_stack2(){
if(top2 < size){
int popped_data = array[top2];
top2++;
cout<<popped_data<<" popped from stack 2"<<endl;
}
else{
cout<<"Stack 2 is Empty!"<<endl;
}
}
/*Function to display all the elements in stack 1*/
void display_stack1(){
cout<<"Displaying elements in Stack 1"<<endl;
for(int i = top1;i>-1;--i){
cout<<array[i]<<" "<<endl;
}
}
/*Function to display all the elements in stack 2*/
void display_stack2(){
cout<<"Displaying elements in Stack 2"<<endl;
for(int i = top2;i<size;++i){
cout<<array[i]<<" "<<endl;
}
}
/*Driver program */
int main(){
int ch;
do{
cout<<"\n---------MENU---------"<<endl;
cout<<"1. Push in stack 1\n2. Push in stack 2\n3. Pop from stack 1\n4. Pop from stack 2\n5. Display stack 1\n6. Display stack 2\n7. Exit"<<endl;
cout<<"Enter your choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
int val;
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push_in_stack1(val);
break;
}
case 2:{
int val;
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push_in_stack2(val);
break;
}
case 3: {
pop_from_stack1();
break;
}
case 4: {
pop_from_stack2();
break;
}
case 5: {
display_stack1();
break;
}
case 6: {
display_stack2();
break;
}
case 7: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=7);
return 0;
}
/*Time Complexity
push - O(1) as we directly push the value at the end of stack.
pop - O(1) as we directly remove the last value at the top of stack.
Space Complexity
O(size)
Sample Output
---------MENU---------
1. Push in stack 1
2. Push in stack 2
3. Pop from stack 1
4. Pop from stack 2
5. Display stack 1
6. Display stack 2
7. Exit
Enter your choice:
1
Enter value to be pushed:
10
10 pushed onto stack 1
---------MENU---------
1. Push in stack 1
2. Push in stack 2
3. Pop from stack 1
4. Pop from stack 2
5. Display stack 1
6. Display stack 2
7. Exit
Enter your choice:
1
Enter value to be pushed:
20
20 pushed onto stack 1
---------MENU---------
1. Push in stack 1
2. Push in stack 2
3. Pop from stack 1
4. Pop from stack 2
5. Display stack 1
6. Display stack 2
7. Exit
Enter your choice:
1
Enter value to be pushed:
30
30 pushed onto stack 1
---------MENU---------
1. Push in stack 1
2. Push in stack 2
3. Pop from stack 1
4. Pop from stack 2
5. Display stack 1
6. Display stack 2
7. Exit
Enter your choice:
3
30 popped from stack 1
---------MENU---------
1. Push in stack 1
2. Push in stack 2
3. Pop from stack 1
4. Pop from stack 2
5. Display stack 1
6. Display stack 2
7. Exit
Enter your choice:
5
Displaying elements in Stack 1
20
10
---------MENU---------
1. Push in stack 1
2. Push in stack 2
3. Pop from stack 1
4. Pop from stack 2
5. Display stack 1
6. Display stack 2
7. Exit
Enter your choice:
2
Enter value to be pushed:
5
5 pushed onto stack 2
---------MENU---------
1. Push in stack 1
2. Push in stack 2
3. Pop from stack 1
4. Pop from stack 2
5. Display stack 1
6. Display stack 2
7. Exit
Enter your choice:
2
Enter value to be pushed:
8
8 pushed onto stack 2
---------MENU---------
1. Push in stack 1
2. Push in stack 2
3. Pop from stack 1
4. Pop from stack 2
5. Display stack 1
6. Display stack 2
7. Exit
Enter your choice:
4
8 popped from stack 2
---------MENU---------
1. Push in stack 1
2. Push in stack 2
3. Pop from stack 1
4. Pop from stack 2
5. Display stack 1
6. Display stack 2
7. Exit
Enter your choice:
6
Displaying elements in Stack 2
5
---------MENU---------
1. Push in stack 1
2. Push in stack 2
3. Pop from stack 1
4. Pop from stack 2
5. Display stack 1
6. Display stack 2
7. Exit
Enter your choice:
7
Exit
*/