-
Notifications
You must be signed in to change notification settings - Fork 29
/
Stack.cpp
54 lines (47 loc) · 1.51 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
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 5
using namespace std;
int main() {
int item, choice, i;
int arr_stack[MAX_SIZE];
int top = 0;
int exit = 1;
cout << "\nSimple Stack Example - Array - C++";
do {
cout << "\n\nnStack Main Menu";
cout << "\n1.Push \n2.Pop \n3.Display \nOthers to exit";
cout << "\nEnter Your Choice : ";
cin>>choice;
switch (choice) {
case 1:
if (top == MAX_SIZE)
cout << "\n## Stack is Full!";
else {
cout << "\nEnter The Value to be pushed : ";
cin>>item;
cout << "\n## Position : " << top << ", Pushed Value :" << item;
arr_stack[top++] = item;
}
break;
case 2:
if (top == 0)
cout << "\n## Stack is Empty!";
else {
--top;
cout << "\n## Position : " << top << ", Popped Value :" << arr_stack[top];
}
break;
case 3:
cout << "\n## Stack Size : " << top;
for (i = (top - 1); i >= 0; i--)
cout << "\n## Position : " << i << ", Value :" << arr_stack[i];
break;
default:
exit = 0;
break;
}
} while (exit);
return 0;
}