-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cpp
78 lines (73 loc) · 1.4 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
#include<iostream>
using namespace std;
# define n 5
int stack[n];
int top = -1;
void push(){
int x;
cout<<"enter the element that you want to enter"<<endl;
cin>>x;
if(top==n-1){
cout<<"stack overflow"<<endl;
}
else{
top++;
stack[top]=x;
}
}
void pop(){
if(top==-1){
cout<<"nothing to pop , stack underflow"<<endl;
}
else{
cout<<"the element that you deleted is "<<stack[top]<<endl;
top--;
}
}
void peek(){
if(top==-1){
cout<<"stack underflow"<<endl;
}
else{
cout<<"the top most element in the stack is"<<stack[top]<<endl;
}
}
void display(){
cout<<"displaying......"<<endl;
int i;
for(i=top;i>=0;i--){
cout<<stack[i]<<endl;
}
}
int main(){
while(1){
int c;
cout<<"press 1 to push"<<endl;
cout<<"press 2 to pop"<<endl;
cout<<"press 3 to peek"<<endl;
cout<<"press 4 to display"<<endl;
cout<<"press 5 to exit the program"<<endl;
cin>>c;
if(c==5){
cout<<"program finished"<<endl;
break;
}
switch(c){
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
default:
cout<<"invalid choice"<<endl;
}
}
return 0;
}