Source Code :
#include<iostream.h>
class stack {
int st[100];
int top;
public:
void init() {
top=0;
}
void push(int var) {
st[++top]=var;
}
int pop() {
return st[top--];
}
};
int main() {
int ch,fl=0,ctr=0;
stack s1;
while(true) {
cout<<endl<<" MAIN MENU";
cout<<endl<<"1. Initialize";
cout<<endl<<"2. Push";
cout<<endl<<"3. Pop";
cout<<endl<<"4. Exit";
cout<<endl<<"Enter Choice : ";
cin>>ch;
switch(ch) {
case 1: {
s1.init();
cout<<endl<<"Stack Initialization Successful"<<endl;
fl=1;
}break;
case 2: {
if(fl==1) {
int n;
cout<<endl<<"Enter Element : ";
cin>>n;
if(ctr<100) {
s1.push(n);
ctr++;
}
else
cout<<endl<<"Stack Overflow"<<endl;
}
else
cout<<endl<<"Stack not initialized"<<endl;
}break;
case 3: {
if(fl==1) {
if(ctr>0) {
cout<<endl<<"Element Popped : "<<s1.pop();
cout<<endl<<"Element Remaining : "<<--ctr;
}
else
cout<<endl<<"Stack Underflow"<<endl;
}
else
cout<<endl<<"Stack not initialized"<<endl;
}break;
case 4: {
exit(0);
}break;
default: {
cout<<endl<<"INVALID CHOICE"<<endl;
}
}
}
}
Output :
MAIN MENU
1. Initialize
2. Push
3. Pop
4. Exit
Enter Choice : 1
Stack Initialization Successful
MAIN MENU
1. Initialize
2. Push
3. Pop
4. Exit
Enter Choice : 2
Enter Element : 4
MAIN MENU
1. Initialize
2. Push
3. Pop
4. Exit
Enter Choice : 2
Enter Element : 8
MAIN MENU
1. Initialize
2. Push
3. Pop
4. Exit
Enter Choice : 3
Element Popped : 8
Element Remaining : 1
MAIN MENU
1. Initialize
2. Push
3. Pop
4. Exit
Enter Choice : 3
Element Popped : 4
Element Remaining : 0
MAIN MENU
1. Initialize
2. Push
3. Pop
4. Exit
Enter Choice : 3
Stack Underflow
No comments:
Post a Comment