Stack: Pop and Push Operations
// Exer16 Stack - Pop & Push Operations together.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 3
class Stack
{
private:
int s[MAX], top;
public:
Stack() // Constructor to initialize TOP pointer
{
top = -1;
}
void push(int);
int pop( ); // Member Functions Declaration
void display( );
};
void Stack::push(int item)
{
if(top == MAX-1)
cout<<"Stack is Full....Overflow!!!"<<endl;
else
{
top++;
s[top]=item;
}
}
int Stack::pop()
{
int item = '\0';
if(top == -1)
cout<<"Stack Empty!!!...Can't POP"<<endl;
else
{
item = s[top];
top--;
}
return item;
}
void Stack::display( )
{
if(top == -1)
cout<<"Stack Empty!!!"<<endl;
else
{
for(int i=0; i<=top; i++)
cout<<endl<<s[i];
cout<<"-->top element"<<endl;
}
}
void main( )
{
Stack s;
int choice, ele, val;
clrscr( );
while(1)
{
cout<<"\n Stack Push & Pop Operation Menu"<<endl;
cout<<"1.PUSH"<<endl;
cout<<"2.POP"<<endl;
cout<<"3.DISPLAY"<<endl;
cout<<"4.EXIT"<<endl;
cout<<"Enter your Choice"<<endl;
cin>>choice;
switch(choice)
{
case 1: cout<<"Push Operation"<<endl;
cout<<"enter the value of an element"<<endl;
cin>>ele;
s.push(ele);
break;
case 2: cout<<"Pop Operation"<<endl;
cout<<"Popped Element is: "<< s.pop();
break;
case 3:
cout<<"Stack elements are:"<<endl;
s.display( );
break;
case 4:
cout<<"End of the Stack Operetion"<<endl;
getch( );
exit(1);
default:
cout<<"Invalid Choice...!!!"<<endl;
break;
}
getch();
}
}
Comments
Post a Comment