-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwo_stack_1_array.cpp
More file actions
91 lines (83 loc) · 1.42 KB
/
two_stack_1_array.cpp
File metadata and controls
91 lines (83 loc) · 1.42 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
#include<bits/stdc++.h>
using namespace std;
class tsc
{
int *a;
int len;
int t1, t2;
public:
tsc(int n)
{
len=n;
a=new int[n];
t1=-1;
t2=len;
}
void push1(int x)
{
if(t1<t2 - 1)
{
t1++;
a[t1]=x;
}
else
{
cout<<"Stack Overflow";
exit(1);
}
}
void push2(int x)
{
if(t1<t2 - 1)
{
t2--;
a[t2]=x;
}
else
{
cout<<"Stack Overflow";
exit(1);
}
}
int pop1()
{
if (t1>=0)
{
int x=a[t1];
t1--;
return x;
}
else
{
cout<<"Stack UnderFlow";
exit(1);
}
}
int pop2()
{
if(t2<len)
{
int x=a[t2];
t2++;
return x;
}
else
{
cout<<"Stack UnderFlow";
exit(1);
}
}
};
int main()
{
tsc ts(5);
ts.push1(1);
ts.push2(2);
ts.push2(3);
ts.push1(4);
ts.push2(5);
cout<<"Element popped from stack1 is "<<ts.pop1()<<endl;
ts.push2(6);
cout<<"Element popped from stack2 is "<<ts.pop2();
return 0;
}