-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.ds_Stack.cpp
104 lines (94 loc) · 1.63 KB
/
16.ds_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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include <cstdlib>
using namespace std;
#define MAX 10
int stack[MAX];
int top=-1;
int isFull(){
if(top==MAX-1)
return 1;
else
return 0;
}
int isEmpty(){
if(top==-1)
return 1;
else
return 0;
}
void push(int item){
if(isFull()){
cout<<"\nOverflow";
return;
}
top += 1;
stack[top]=item;
}
int pop(){
int item;
if(isEmpty()){
cout<<"\nUnderflow";
exit(1);
}
item=stack[top];
top -= 1;
return item;
}
int peek(){
if(isEmpty()){
cout<<"\nUnderflow";
exit(1);
}
return stack[top];
}
void display(){
int i;
if(isEmpty()){
cout<<"\nStack empty";
return;
}
cout<<"\nStack: ";
for(i=top;i>=0;i--){
cout<<stack[i]<<" ";
}
}
int main(){
int choice;
while(1){
cout<<"\n_____MENU_____\n";
cout<<"1.Push\n";
cout<<"2.Pop\n";
cout<<"3.Peek Top\n";
cout<<"4.Display All\n";
cout<<"5.Quit\n";
cout<<"Enter choice: ";
cin>>choice;
int item;
switch(choice){
case 1:
cout<<"\nEnter item to push: ";
cin>>item;
push(item);
break;
case 2:
item=pop();
cout<<"\npopped: "<<item;
break;
case 3:
item=peek();
cout<<"\nTop: "<<item;
break;
case 4:
display();
break;
case 5:
exit(1);
default:
cout<<"\nwrong choice";
}
}
return 0;
}