-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplement stack and so its push, pop, and peek operation..cpp
81 lines (70 loc) · 1.53 KB
/
implement stack and so its push, pop, and peek operation..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
#include <iostream>
using namespace std;
int stack[100], n = 100, top = -1;
void push(int val) {
if(top >= n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top] = val;
}
}
void pop() {
if(top <= -1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top >= 0) {
cout<<"Stack elements are:";
for(int i = top; i>= 0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty\n";
}
void peek() {
if(top == -1){
cout<<"Stack is empty\n";
}
else
cout<<"The top element is: "<< stack[top] <<endl;
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Display Top element of the stack"<<endl;
cout<<"5) Exit"<<endl;
do {
cout<<"\nEnter choice: ";
cin>>ch;
switch(ch) {
case 1:
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
peek();
break;
case 5:
cout<<"Exit"<<endl;
break;
default:
cout<<"Invalid Choice"<<endl;
}
}
while(ch!=5);
return 0;
}