-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstackSimplified.cpp
103 lines (76 loc) · 1.42 KB
/
stackSimplified.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<iostream>
#include<string>
#include<stack>
# define max_size 101
using namespace std;
//declaring stack array
int Stack[max_size];
int top=-1; //initially
//declaring stack operations
void push(int x) {
if(top==max_size-1) {
cout<<"Sorry!, the stack is full"<<endl;
}
//push into stack
cout<<"Enter element to insert"<<endl;
cin>>x;
//first increment top pointer and push element
Stack[++top] = x;
}
void pop() {
if(top==-1) {
cout<<"Stack is empty"<<endl;
return;
}
//pop and then decrement top
cout<<"Deleted number is: "<<Stack[top]<<endl;
top--;
}
bool isEmpty() {
if(top==-1) return true;
return false;
}
void display() {
if(isEmpty()) {
cout<<"ERROR: stack is empty! "<<endl;
}
else if(!isEmpty()) {
int i;
cout<<"Stack :"<<endl;
for(i=top;i>=0;i--) {
cout<<Stack[i]<<endl;
}
}
}
void Top() {
if(top==-1) cout<<"Stack is empty"<<endl;
if(top==max_size-1) cout<<"Stack is full"<<endl;
cout<<Stack[top]<<endl;
}
int main () {
int choice;
while(true) {
cout<<"-----------------MENU---------------"<<endl;
cout<<"1)Insert\n2)Delete\n3)Print top\n4)Display stack\n5)Exit"<<endl;
cin>>choice;
switch(choice) {
case 1:
int data;
push(data);
break;
case 2:
pop();
break;
case 3:
Top();
break;
case 4:
display();
break;
default:
exit(0);
break;
}
}
return 0;
}