-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplement_two_Stacks_in_an_array.cpp
More file actions
112 lines (103 loc) · 1.71 KB
/
Copy pathImplement_two_Stacks_in_an_array.cpp
File metadata and controls
112 lines (103 loc) · 1.71 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// @author: Abhimanyu Maurya
#include <iostream>
using namespace std;
//fast i/o
bool ib = ios_base::sync_with_stdio(0);
bool it = cin.tie(0);
bool ot = cout.tie(0);
int stack[1000000], top1, top2, N;
int stkTop(int stkID)
{
if (stkID == 1)
return stack[top1];
else
return stack[top2];
}
bool isEmpty(int stkID)
{
if (stkID == 1 and top1 == -1)
return true;
if (stkID == 2 and top2 == N)
return true;
return false;
}
void stkPop(int stkID)
{
int t;
if (stkID == 1)
{
t = stack[top1];
top1--;
}
if (stkID == 2)
{
t = stack[top2];
top2++;
}
}
void stkPush(int stkID, int data)
{
if (stkID == 1)
{
top1++;
stack[top1] = data;
}
if (stkID == 2)
{
top2--;
stack[top2] = data;
}
}
void stkPrint(int stkID)
{
while (!isEmpty(stkID))
{
cout << stkTop(stkID) << ' ';
stkPop(stkID);
}
}
int main()
{
int n, m, t;
cin >> N >> n >> m;
top1 = -1;
top2 = N;
for (int i = 0; i < n; i++)
{
cin >> t;
stkPush(1, t);
}
for (int i = 0; i < m; i++)
{
cin >> t;
stkPush(2, t);
}
if (n == 0)
cout << "None";
else
{
cout << stkTop(1) << '\n';
stkPop(1);
}
if (m == 0)
cout << "None";
else
{
cout << stkTop(2) << '\n';
stkPop(2);
}
cout << "Elements in stack1 are\n";
if (isEmpty(1))
cout
<< "None";
else
stkPrint(1);
cout << "\n";
cout << "Elements in stack2 are\n";
if (isEmpty(2))
cout
<< "None";
else
stkPrint(2);
return 0;
}