-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array_Stack.cpp
219 lines (196 loc) · 3.31 KB
/
Array_Stack.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <iostream>
#include <iomanip>
using namespace std;
const int size = 10;
template <class T>
class stacktype{
T s[size];
int top;
public :
void push(T);
T pop();
int isempty();
int isfull();
void display();
T topmost();
void clear();
stacktype()
{
top = -1;
}
};
template <class T>
void stacktype<T>::push(T p)
{
top++;
s[top] = p;
}
template <class T>
T stacktype<T>::pop()
{
int d;
d = s[top];
top--;
return d;
}
template <class T>
int stacktype<T>::isempty()
{
int e;
if(top == -1)
{
cout<<"\nStack is empty"<<endl;
return 1;
}
else
{
cout<<"\nStack is not empty"<<endl;
return 0;
}
}
template <class T>
int stacktype<T>::isfull()
{
int f;
if(top == size-1)
{
cout<<"\nStack is full"<<endl;
return 1;
}
else
{
cout<<"\nStack is not full"<<endl;
return 0;
}
}
template <class T>
T stacktype<T>::topmost()
{
return s[top];
}
template <class T>
void stacktype<T>::clear()
{
top = -1;
display();
}
template <class T>
void stacktype<T>::display()
{
if(top == -1)
{
cout<<"\nStack is empty"<<endl;
}
else
{
cout<<"\nStack elements starting from top are - ";
for(int i=top;i>=0;i--)
{
cout<<setw(3)<<s[i];
}
cout<<endl;
}
}
int main()
{
stacktype<int> st;
char c = 'y';
int choice,f,p,d,e;
while(c=='y'||c=='Y')
{
cout<<"MAIN MENU :"<<endl;
cout<<"From the following options which operation you want to perform on stack :"<<endl;
cout<<"1. PUSH onto the stack "<<endl;
cout<<"2. POP from the stack "<<endl;
cout<<"3. Check if stack is EMPTY "<<endl;
cout<<"4. Check if stack is FULL "<<endl;
cout<<"5. CLEAR the stack "<<endl;
cout<<"6. TOPMOST element of stack "<<endl;
cout<<"7. DISPLAY the stack "<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
f = st.isfull();
if(f==1) //f==1 implies stack is full
{
cout<<endl;
cout<<"Insertion of a new element is not possible"<<endl; //overflow condition
}
else
{
cout<<"Enter the element you want to push- ";
cin>>p;
st.push(p);
st.display();
}
break;
case 2:
e = st.isempty();
if(e==1) //e==1 implies stack is empty
{
cout<<endl;
cout<<"Deletion not possible"<<endl; //Underflow condition
}
else
{
d = st.pop();
cout<<"\n"<<d<<" has been deleted";
st.display();
}
break;
case 3:
e = st.isempty();
if(e==1)
{
cout<<endl;
}
else
{
cout<<endl;
st.display();
}
break;
case 4:
f = st.isfull();
if(f==1)
{
cout<<endl;
st.display();
}
else
{
cout<<endl;
}
break;
case 5:
st.clear();
cout<<"\nStack is cleared!!"<<endl;
break;
case 6:
e = st.isempty();
if(e == 1)
{
cout<<endl;
}
else
{
cout<<"The topmost element of the stack is - ";
cout<<st.topmost()<<endl;
}
/*cout<<"The topmost element of the stack is - ";
cout<<st.topmost()<<endl;*/
break;
case 7:
st.display();
break;
default:
cout<<"Invalid Choice!!"<<endl;
break;
}
cout<<"\nDo you want to continue ??(Y/N): ";
cin>>c;
}
return 0;
}