Skip to content

Commit d8738cb

Browse files
authored
Merge pull request #199 from aniket-41/patch-1
2Stacks in an array
2 parents 52c2175 + 2cd5c98 commit d8738cb

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed

2StackInAnArray.cpp

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#include <iostream>
2+
#include <stdlib.h>
3+
4+
using namespace std;
5+
6+
class twoStacks
7+
{
8+
int *arr;
9+
int size;
10+
int top1, top2;
11+
12+
public:
13+
twoStacks(int n)
14+
{
15+
size = n;
16+
arr = new int[n];
17+
top1 = -1;
18+
top2 = size;
19+
}
20+
21+
void push1(int x)
22+
{
23+
if (top1 < top2 - 1)
24+
{
25+
top1++;
26+
arr[top1] = x;
27+
}
28+
else
29+
{
30+
cout << "Stack Overflow";
31+
exit(1);
32+
}
33+
}
34+
35+
void push2(int x)
36+
{
37+
38+
if (top1 < top2 - 1)
39+
{
40+
top2--;
41+
arr[top2] = x;
42+
}
43+
else
44+
{
45+
cout << "Stack Overflow";
46+
exit(1);
47+
}
48+
}
49+
50+
int pop1()
51+
{
52+
if (top1 >= 0)
53+
{
54+
int x = arr[top1];
55+
top1--;
56+
return x;
57+
}
58+
else
59+
{
60+
cout << "Stack UnderFlow";
61+
return -1;
62+
}
63+
}
64+
65+
int pop2()
66+
{
67+
if (top2 < size)
68+
{
69+
int x = arr[top2];
70+
top2++;
71+
return x;
72+
}
73+
else
74+
{
75+
cout <<endl<< "Stack UnderFlow";
76+
return -1;
77+
}
78+
}
79+
void display()
80+
{
81+
cout << "Stack 1 ";
82+
if (top1 >= 0)
83+
{
84+
for (int i = top1; i >= 0; i--)
85+
cout << arr[i] << " ";
86+
}
87+
else
88+
cout << "Stack is empty";
89+
90+
cout << endl;
91+
cout << "Stack 2: " ;
92+
if (top2 != size)
93+
{
94+
for (int i = top2; i < size; i++)
95+
{
96+
cout << arr[i] << " ";
97+
}
98+
}
99+
else
100+
{
101+
cout << "Stack is empty";
102+
}
103+
cout << endl;
104+
}
105+
};
106+
107+
int main()
108+
{
109+
int n;
110+
cout << "Enter the size of the stack ";
111+
cin >> n;
112+
113+
twoStacks ts(n);
114+
int ch, val;
115+
116+
do
117+
{
118+
cout << "1) Push in stack 1" << endl;
119+
cout << "2) Push in stack 2" << endl;
120+
cout << "3) Pop from stack 1" << endl;
121+
cout << "4) Pop from stack 2" << endl;
122+
cout << "5) Display Stack" << endl;
123+
cout << "6) Exit" << endl;
124+
cout << "Enter choice: " << endl;
125+
cin >> ch;
126+
switch (ch)
127+
{
128+
case 1:
129+
{
130+
cout << "Enter value to be pushed in stack 1:" << endl;
131+
cin >> val;
132+
ts.push1(val);
133+
break;
134+
}
135+
case 2:
136+
{
137+
cout << "Enter value to be pushed in stack 2: " << endl;
138+
cin >> val;
139+
ts.push2(val);
140+
break;
141+
}
142+
case 3:
143+
{
144+
145+
int x = ts.pop1();
146+
if (x != -1)
147+
{
148+
cout << "Popped Value from stack 1 is " << x << endl;
149+
break;
150+
}
151+
break;
152+
}
153+
case 4:
154+
{
155+
156+
int x = ts.pop2();
157+
if (x != -1)
158+
{
159+
cout << "Popped Value from stack 2 is " << x << endl;
160+
break;
161+
}
162+
break;
163+
}
164+
165+
case 5:
166+
{
167+
ts.display();
168+
break;
169+
}
170+
case 6:
171+
{
172+
cout << "Exit" << endl;
173+
break;
174+
}
175+
default:
176+
{
177+
cout << "Invalid Choice" << endl;
178+
}
179+
}
180+
181+
} while (ch != 6);
182+
}

0 commit comments

Comments
 (0)