-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.cpp
72 lines (69 loc) · 1.06 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
#include<iostream>
#include<cstdio>
#include<string>
#include<ctype.h>
using namespace std;
#define size 10
struct stack
{
int a[size];
int top;
}s1;
void push(int ele)
{
if (s1.top==size)
{
cout<<"\noverflow\n";
}
else
{
s1.a[++s1.top]=ele;
cout<<"\nelement inserted sucessfully\n";
}
}
int pop()
{
if (s1.top==-1)
{
cout<<"\nunderflow \nnothing to delete\n";
return -1;
}
else
{ int b;
cout<<"element deleted sucessfully";
b=s1.a[s1.top];
s1.top--;
return b;
}
}
void disptop()
{
cout<<s1.a[s1.top-1];
}
int main()
{
s1.top=-1;
int ele,choice,ret_ele;
while(1)
{
cout<<"\n1.push\n2.pop\n3.Display top\n4.exit\n";
cout<<"\nenter the choice\t";
cin>>choice;
switch(choice)
{
case 1 : cout<<"\nenter the element to insert\t";
cin>>ele;
push(ele);
break;
case 2 : ret_ele=pop();
cout<<"\n deleted element is "<<ret_ele;
break;
case 3 : disptop();
break;
case 4 : exit(0);
default : cout<<"\nenter the correct choice\t";
cin>>choice;
}
}
return 0;
}