-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_prac.cpp
More file actions
69 lines (64 loc) · 1.16 KB
/
stack_prac.cpp
File metadata and controls
69 lines (64 loc) · 1.16 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
#include <iostream>
using namespace std;
#define MAX 10
int top=-1;
int a[MAX];
void push(int d){
if(top>=(MAX-1)){
cout << "Stack Overflow" << '\n';
}
else{
a[++top]=d;
cout<<d<< " insert successfully"<<'\n';
}
}
void pop(){
if(top<=-1){
cout << "Stack Underflow" << '\n';
}
else{
top--;
cout << "Poped successfully" << '\n';
}
}
int peek(){
return a[top];
}
void show(){
cout << "==================Data is======================" << '\n';
if(top==-1){
cout << "Stack is empty" << '\n';
}
else{
for (int i = 0; i <= top; i++) {
cout<<a[i]<<'\n';
}
}
}
int main() {
int choice;
int data;
while (choice!=5) {
cout << "Enter 1 to push : " << '\n';
cout << "Enter 2 to pop element : " << '\n';
cout << "Enter 3 to peek element : " << '\n';
cout << "Enter 4 view data : " << '\n';
cout << "Enter 5 to exit : " << '\n';
cin>>choice;
if (choice==1) {
cout << "Enter data : " << '\n';
cin >> data;
push(data);
}
else if (choice==2) {
pop();
}
else if (choice==3) {
cout<<peek()<<'\n';
}
if (choice==4) {
show();
}
}
return 0;
}