-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack operations using static array.cpp
93 lines (82 loc) · 1.19 KB
/
stack operations using static 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<iostream>
#define capacity 100
using namespace std;
int stack[capacity],top=-1;
int isfull()
{
if(top==capacity-1)
return 1;
else
return 0;
}
int isempty()
{
if(top==-1)
return 1;
else
return 0;
}
void pop()
{
if(isempty())
cout<<"stack is empty\n";
else
{
cout<<stack[top]<<endl;
top--;
}
}
void push(int ele)
{
if(isfull())
{
cout<<"stack is full\n";
}
else
{
top++;
stack[top]=ele;
cout<<"element pushed successsfully\n";
}
}
void print()
{
int x=top;
if(isempty())
{
cout<<"stack is empty\n";
}
else
{
while(x!=-1)
{
cout<<stack[x]<<" ";
x--;
}
}
}
int main()
{
int element,t;
loop:
cout<<"\nenter 1 to push the element on to the stack\n";
cout<<"enter 2 to pop the element from the stack\n";
cout<<"enter 3 to print all the elements of the stack\n";
cout<<"enter 4 to exit\n";
cout<<"enter ur choice\n";
cin>>t;
switch(t)
{
case 1:cout<<"enter the value to be pushed\n";
cin>>element;
push(element);
break;
case 2:pop();
break;
case 3:print();
break;
case 4:exit(0);
}
goto loop;
return 0;
}