forked from srinidh-007/Coding_Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementation_of_Two_Stacks_in_one_Array.cpp
More file actions
96 lines (86 loc) · 1.51 KB
/
Implementation_of_Two_Stacks_in_one_Array.cpp
File metadata and controls
96 lines (86 loc) · 1.51 KB
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
#include<bits/stdc++.h>
using namespace std;
class Two_Stacks
{
int *arr;
int t, m, n;;
public:
Two_Stacks(int num)
{
t=num;
arr=new int[num];
m=-1;
n=t;
}
void push1(int x)
{
if(m<(n - 1))
{
m++;
arr[m]=x;
}
else
{
cout<<"Stack Overflow"<<endl;
exit(1);
}
}
void push2(int x)
{
if(m<(n - 1))
{
n--;
arr[n]=x;
}
else
{
cout<<"Stack Overflow"<<endl;
exit(1);
}
}
int pop1()
{
if (m>=0)
{
int temp=arr[m];
m--;
return temp;
}
else
{
cout<<"Stack UnderFlow"<<endl;
exit(1);
}
}
int pop2()
{
if(n<t)
{
int x=arr[n];
n++;
return x;
}
else
{
cout<<"Stack UnderFlow"<<endl;
exit(1);
}
}
};
int main()
{
Two_Stacks obj(8);
obj.push1(5);
obj.push2(10);
obj.push1(15);
obj.push2(20);
obj.push2(25);
obj.push1(30);
obj.push1(35);
cout<<"Element popped from stack1 is "<<obj.pop1()<<endl;
obj.push2(50);
cout<<"Element popped from stack2 is "<<obj.pop2()<<endl;
obj.push1(55);
cout<<"Element popped from stack1 is "<<obj.pop1()<<endl;
return 0;
}