-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTwoStacksInOneArray.java
More file actions
42 lines (38 loc) · 925 Bytes
/
TwoStacksInOneArray.java
File metadata and controls
42 lines (38 loc) · 925 Bytes
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
/*https://practice.geeksforgeeks.org/problems/implement-two-stacks-in-an-array/1*/
class Stacks
{
//Function to push an integer into the stack1.
void push1(int x, TwoStack sq)
{
++sq.top1;
if (sq.top1 == sq.top2)
{
--sq.top1;
return;
}
sq.arr[sq.top1] = x;
}
//Function to push an integer into the stack2.
void push2(int x, TwoStack sq)
{
--sq.top2;
if (sq.top1 == sq.top2)
{
++sq.top2;
return;
}
sq.arr[sq.top2] = x;
}
//Function to remove an element from top of the stack1.
int pop1(TwoStack sq)
{
if (sq.top1 == -1) return -1;
return sq.arr[sq.top1--];
}
//Function to remove an element from top of the stack2.
int pop2(TwoStack sq)
{
if (sq.top2 == sq.size) return -1;
return sq.arr[sq.top2++];
}
}