-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
155 lines (133 loc) Β· 5.64 KB
/
Stack.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
using namespace std;
#define SIZE 15
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
class STACK{
private:
int stack[SIZE];
int top;
public:
STACK(); //defualt constructor
int push(int);
int pop();
bool isEmpty();
int isFull();
void displayItems();
};
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
STACK :: STACK(){
top =- 1;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
bool STACK :: isEmpty(){
if(top == -1)
return true;
else
return false;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
int STACK :: isFull(){
if(top == (SIZE - 1))
return 1;
else
return 0;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
int STACK :: push(int n){
// first, check stack is full
if(isFull())
return 0;
++top;
stack[top] = n;
return n;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
int STACK :: pop(){
// to store and print which number is deleted
int temp;
// check if empty
if(isEmpty())
return 0;
temp = stack[top];
--top;
return temp;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void STACK :: displayItems(){
int i;
if (top == -1){
cout << "STACK is empty" << endl << endl;
}
else{
for(i = top; i >= 0; i--)
cout << stack[i] << " ";
cout << endl << endl;
}
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
// Objects
STACK stk1;
STACK stk2;
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
int main(){
int choice, n, temp;
cout << "0 - Exit." << endl;
cout << "1 - Push Item in Stack 1." << endl;
cout << "2 - Push Item in Stack 2." << endl;
cout << "3 - Pop Item from Stack 1." << endl;
cout << "4 - Pop Item from Stack 2." << endl;
cout << "5 - Display Stack 1." << endl;
cout << "6 - Display Stack 2." << endl << endl;
do{
cout << "Enter your choice : ";
cin >> choice;
switch(choice){
case 0:
break;
case 1:
cout << "Enter item to insert : ";
cin >> n;
temp = stk1.push(n);
if(temp == 0)
cout << "STACK is FULL." << endl << endl;
else
cout << temp << " is inserted (pushed) in Stack 1." << endl << endl;
break;
case 2:
cout << "Enter item to insert : ";
cin >> n;
temp = stk2.push(n);
if(temp == 0)
cout << "STACK is FULL." << endl << endl;
else
cout << temp << " is inserted (pushed) in Stack 2." << endl << endl;
break;
case 3:
temp = stk1.pop();
if(temp == 0)
cout << "STACK is EMPTY." << endl << endl;
else
cout << temp << " is removed (popped) from Stack 1." << endl << endl;
break;
case 4:
temp = stk2.pop();
if(temp == 0)
cout << "STACK is EMPTY." << endl << endl;
else
cout << temp << " is removed (popped) from Stack 2." << endl << endl;
break;
case 5:
cout << "STACK 1 :\n";
stk1.displayItems();
break;
case 6:
cout << "STACK 2 :\n";
stk2.displayItems();
break;
default:
cout << "Invalid choice." << endl;
}
}while(choice != 0);
return 0;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/