forked from imnurav/Hactoberfest2021-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_implementation.cpp
78 lines (77 loc) · 1.03 KB
/
stack_implementation.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<bits/stdc++.h>
#define max 100
using namespace std;
int stk[max],top=-1;
void push()
{
int x;
if(top>max)
{
cout<<"Overflow Condition\n";
return;
}
top++;
cout<<"Enter the element\n";
cin>>x;
stk[top]=x;
}
void pop()
{
if(top<0)
{
cout<<"Stack is empty\n";
return;
}
cout<<"Pop element:";
cout<<stk[top];
top--;
cout<<endl;
}
void peep()
{
if(top<0)
{
cout<<"Stack is empty\n";
return;
}
cout<<"Top element of stack:";
cout<<stk[top];
cout<<endl;
}
void size()
{
if(top<0)
{
cout<<"Stack is empty\n";
return;
}
cout<<"Size of stack:"<<top+1<<endl;
for(int i=0;i<=top;i++)
cout<<stk[i]<<" ";
cout<<endl;
}
int main()
{
int op;
do{
cout<<"...........Menu.........\n";
cout<<"\n1)push()\n 2)pop()\n 3)top() \n4)size()\n5) 0 for exit\n";
cout<<"Enter option:";
cin>>op;
switch(op)
{
case 1:push();
break;
case 2:pop();
break;
case 3:peep();
break;
case 4:size();
break;
case 0:cout<<"BYE \n";
break;
default:
cout<<"Invalid option\n";
}
}while(op!=0);
}