-
Notifications
You must be signed in to change notification settings - Fork 18
/
Stacks_ArrayImplementation.cpp
98 lines (83 loc) · 1.83 KB
/
Stacks_ArrayImplementation.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
#include <iostream>
#define SIZE 150
using namespace std;
int stack[SIZE], top = -1; // Initialise a stack of SIZE = 150 and top = -1
bool isEmpty(){
if (top == -1)
return true;
else
return false;
}
bool isFull(){
if (top >= SIZE - 1)
return true;
else
return false;
}
void push(int val){
if (isFull())
cout << "Stack overflow!" << endl;
else{
stack[++top] = val;
cout << "Element Pushed!" << endl;
}
}
void pop(){
if (isEmpty())
cout << "Stack underflow!" << endl;
else{
int val = stack[top--];
cout << val << " Popped!" << endl;
}
}
void StackTop(){
if (isEmpty())
cout << "Stack underflow!" << endl;
else
cout << "Element at top: " << stack[top] << endl;
}
void StackSize(){
cout << "Stack Size: " << top + 1 << endl;
}
int main(){
int q = 1;
while(q != 5){
cout << endl;
cout << "1. Push" << endl;
cout << "2. Pop" << endl;
cout << "3. Top" << endl;
cout << "4. Size" << endl;
cout << "5. Exit" << endl;
cout << "Enter your query: ";
cin >> q;
switch(q){
case 1:{
int val;
cout << "Enter the value: ";
cin >> val;
push(val);
break;
}
case 2:{
pop();
break;
}
case 3:{
StackTop();
break;
}
case 4:{
StackSize();
break;
}
case 5:{
exit(1);
break;
}
default:{
cout << "Enter a valid operation!" << endl;
}
}
}
return 0;
}