-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2stacksinArray.cpp
54 lines (49 loc) · 1017 Bytes
/
2stacksinArray.cpp
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
class TwoStack{
int *arr;
int top1;
int top2;
int size;
public:
//Initialize TwoStack.
TwoStack(int s){
this -> size = s;
top1 = -1;
top2 = s;
}
//push in stack 1
void push1(int num){
//atleast a empty space present
if(top2 - top1 > 1){
top1++;
arr[top1] = num;
}
return -1;
}
//push in stack 2
void push2(int num){
if(top2 - top1 > 1){
top2--;
}
return -1;
}
//pop from stack1 and return popped element
int pop1(){
if(top1 >= 0){
int ans = arr[top1];
top1--;
return ans;
}
return -1;
}
//pop from stack 2 and return popped element
int pop2() {
if(top2 < size){
int ans = arr[top2];
top2++;
return ans;
}
else{
return -1;
}
}
};