-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmulStack.cpp
More file actions
82 lines (67 loc) · 1.93 KB
/
mulStack.cpp
File metadata and controls
82 lines (67 loc) · 1.93 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
#include <bits/stdc++.h>
using namespace std;
#define deb(x) cout << #x << " " << x << endl;
class mulStk{
private:
int size = 10;
int top1 = -1;
int top2 = 10;
int *s = new int[10];
public:
void push1(int d){
if(top1 == top2-1) cout << "no space" << endl;
else s[++top1] = d;
}
void push2(int d){
if(top1 == top2-1) cout << "no space" << endl;
else s[--top2] = d;
}
int pop1(){
if(top1 == -1) return INT_MIN;
else return s[top1--];
}
int pop2(){
if(top1 == size) return INT_MIN;
else return s[top2++];
}
void trv1(){
int temp = top1;
while(temp > -1) cout << s[temp--] << " "; cout << endl;
}
void trv2(){
int temp = top2;
while(temp < size) cout << s[temp++] << " "; cout << endl;
}
};
int main(){
mulStk *ms = new mulStk();
cout << "press->> 1 to insert in stk 1, 2 to insert in stk 2; 3 to pop from 1, 4 to pop form 2; 5 to print 1; 6 to print 2 " << endl;
int flag, dtemp;
do{
cin >> flag;
switch(flag){
case 1:
cin >> dtemp;
ms->push1(dtemp);
break;
case 2:
cin >> dtemp;
ms->push2(dtemp);
break;
case 3:
dtemp = ms->pop1();
dtemp == INT_MIN ? cout << "Stack is empty" << endl : cout << dtemp << endl;
break;
case 4:
dtemp = ms->pop2();
dtemp == INT_MIN ? cout << "Stack is empty" << endl : cout << dtemp << endl;
break;
case 5:
ms->trv1();
break;
case 6:
ms->trv2();
}
}while(flag != 0);
return 0;
}