-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplement two stacks in an array
62 lines (60 loc) · 1.14 KB
/
Implement two stacks in an array
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
// Implement two stacks in an array - easy - 07/08/2023
class twoStacks
{
int arr[];
int size;
int top1, top2;
twoStacks()
{
size = 100;
arr = new int[100];
top1 = -1;
top2 = size;
}
//Function to push an integer into the stack1.
void push1(int x)
{
if (top1 < top2 - 1) {
top1++;
arr[top1] = x;
}
else {
return;
}
}
//Function to push an integer into the stack2.
void push2(int x)
{
if (top1 < top2 - 1) {
top2--;
arr[top2] = x;
}
else {
return ;
}
}
//Function to remove an element from top of the stack1.
int pop1()
{
if (top1 >= 0) {
int x = arr[top1];
top1--;
return x;
}
else {
return -1;
}
}
//Function to remove an element from top of the stack2.
int pop2()
{
if (top2 < size) {
int x = arr[top2];
top2++;
return x;
}
else {
return -1;
}
}
}