-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacks.cpp
67 lines (61 loc) · 1.5 KB
/
stacks.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
#include<iostream>
using namespace std;
int s[10],top=-1,max=9;
//push
void push(int x) {
if(top>=9)
cout << "Stack overflow" << endl;
else{
top++;
s[top] = x;
}
}
void pop()
{
if(top<=-1)
cout << "Stack Underflow" << endl;
else{
cout << "popping out" << s[top] << endl;
top--;
}
}
void disp(){
if(top>=0){
cout << "The stack elements are" << endl;
for (int j = top; j >=0;j--){
cout << s[j] << " ";
cout << endl;
}
}else
cout << "Stack is empty" << endl;}
int main(){
int choice;
cout << "1 for push || 2 for pop || 3 for display || 4 for end" << endl;
do{
cin >> choice;
switch(choice){
case 1:{
int x;
cin >> x;
push(x);
break;
}
case 2:{
pop();
break;
}
case 3:{
disp();
break;
}
case 4:{
cout << "Exit" << endl;
break;
}
default:{
cout << "Invalid Choice";
}
}
} while (choice != 4);
return 0;
}