forked from piyush-kash/Hacktober2021-cpp-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
STACK using array.cpp
72 lines (66 loc) · 932 Bytes
/
STACK using array.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<bits/stdc++.h>
#define MAX 10
using namespace std;
class Stack
{
int stack[MAX];
int top;
public:
Stack() {
top=-1;
}
int push(int e);
int pop()
{
if(top<0)
{
cout<<"Stack Underflow\n";
return 0;
}
top--;
return(stack[top+1]);
}
int traverse()
{
for(int i=top;i>=0;i--)
cout<<stack[i]<<" ";
cout<<endl;
}
};
int Stack::push(int e)
{
if(top+1>=MAX)
{
cout<<"Stack Overflow\n";
return 0;
}
top++;
stack[top]=e;
}
int main()
{
int n=0,e=0;
Stack s;
while(1)
{
cout<<"1.Push 2.Pop 3.Traverse 4.Exit\n";
cin>>n;
switch(n)
{
case 1:
cin>>e;
s.push(e);
break;
case 2:
s.pop();
break;
case 3:
s.traverse();
break;
case 4:
return 0;
default:
cout<<"Please choose b/w 1/2/3/4\n";
}
}
}