-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.cpp
72 lines (60 loc) · 980 Bytes
/
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
#include <iostream>
#define MAX 5
using namespace std;
void push(int *a, int m, int& top)
{
if(top == MAX - 1)
cout<<"Stack full"<<endl;
else
a[++top] = m;
}
int pop(int *a, int& top)
{
if(top == -1)
{
cout<<"Stack empty";
return -1;
}
else
{
int x = a[top];
a[top--] = 0;
return x;
}
}
void display(int *a, int top)
{
int i;
if(top == -1)
{
cout<<"Stack empty";
}
for (i = top; i >= 0 ; i--)
cout<<a[i]<<" ";
}
int main()
{
int a[MAX],choice, top=-1, x;
do
{
cout<<"1 to push, 2 to pop, 3 to display, 4 to exit"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cin>>x;
push(a,x,top);
break;
case 2:
x = pop(a,top);
cout<<x<<endl;
break;
case 3:
display(a,top);
break;
case 4:
return 0;
}
}
while(true);
}