-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstack.cpp
More file actions
63 lines (63 loc) · 1.23 KB
/
stack.cpp
File metadata and controls
63 lines (63 loc) · 1.23 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
#include <iostream>
using namespace std;
int stack[5], n=5, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<"\n";
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<"\n";
else {
cout<<"The popped element is "<< stack[top] <<"\n";
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<"\n";
} else
cout<<"Stack is empty";
}
int main() {
int choice, val;
cout<<"1) Push in stack"<<"\n";
cout<<"2) Pop from stack"<<"\n";
cout<<"3) Show stack"<<"\n";
cout<<"4) Exit"<<"\n";
do {
cout<<"Enter choice: "<<"\n";
cin>>choice;
switch(choice) {
case 1: {
cout<<"Enter value to be pushed:"<<"\n";
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<"\n";
break;
}
default: {
cout<<"Invalid Choice"<<"\n";
}
}
}while(choice!=4);
return 0;
}