forked from lucifer-T56/HacktoberFest22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackimplementationusingArray.cpp
116 lines (109 loc) · 1.51 KB
/
stackimplementationusingArray.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
using namespace std;
#define MAX 5
int stack[MAX];
int top = -1;
int isFull()
{
if (top == MAX - 1)
{
return 1;
}
else
{
return 0;
}
}
int isEmpty()
{
if (top == -1)
{
return 1;
}
else
{
return 0;
}
}
void peek()
{
if (isEmpty()){
cout<<"stack is empty\n";
}
else
{
cout<< stack[top];
}
}
void push(int x)
{
if (isFull())
{
cout<<"Overflow\n";
}
else
{
top++;
stack[top] = x;
}
}
void pop()
{
int item;
if (isEmpty())
{
cout<<"underflow\n";
}
else
{
item = stack[top];
top--;
cout<< item;
}
}
void display()
{
int i;
if (top >= 0)
{
cout<<"STACK :";
for (i = top; i >= 0; i--)
cout<< stack[i]<<endl;
}
else
{
cout<<"The STACK is empty\n";
}
}
int main()
{
int ch;
do
{
cout<<"Enter choice: 1:push 2:pop 3:peek 4:display 0.Exit\n";
cin>>ch;
switch (ch)
{
case 1:
{
int x;
cout<<"Enter data: ";
cin>>x;
push(x);
}
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
default:
cout<<"Invalid choice\n";
}
} while (ch != 0);
return 0;
}