-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack-PushPop.cpp
More file actions
70 lines (63 loc) · 1.86 KB
/
Stack-PushPop.cpp
File metadata and controls
70 lines (63 loc) · 1.86 KB
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
#include <iostream>
using namespace std;
struct Stack {
private :
int array[5];
int top = -1;
public :
void push(int num){
if (top < 4){ //OVERFLOW
top++;
array[top] = num;
}
}
int pop(){
if (top == -1){ //UNDERFLOW
return 0;
}
else {
int removed = array[top];
top--;
return removed;
}
}
int showTop(){
return top;
}
};
int main()
{
int choice = 0;
Stack stack1;
int element;
while(choice != 3){
cout<<"Press 1 for push, 2 for pop and 3 for exit "<<endl;
cin>>choice;
if (choice == 1){
if(stack1.showTop() == 4){
cout<<"Stack is full, no more elements can be stored in it"<<endl;
} else {
cout<<endl<<"Enter a number to push "<<endl;
cin>>element;
stack1.push(element);
cout<<"The value of top is "<<stack1.showTop()<<endl;
}
}
else if (choice == 2){
element = stack1.pop();
if(stack1.showTop() == -1){
cout<<"Stack is empty, no elements stored so nothing to pop"<<endl;
}
else {
cout<<"The top most element was "<<element<<endl;
cout<<"The value of top is "<<stack1.showTop()<<endl;
}
}
else if (choice == 3){
cout<<"Exited";
}
else {
cout<<"Please read the instructions carefully and retry";
}
}
}