#include<iostream>
using namespace std;
#define max 5
int a[max];
int top=-1;
int input;
void push()
{
if(top==max-1)
{
cout<<"stack is overflow"<<endl;
}
else
{
cout<<"Enter the Element in the Stack"<<endl;
cin>>input;
top=top+1;
a[top]=input;
}
}
void pop()
{
if(top==-1)
{
cout<<"Stack is Empty"<<endl;
}
else
{
cout<<"Stack value : "<<a[top]<<endl;
top=top-1;
}
}
void display()
{
if(top==-1)
{
cout<<"Stack is Empty"<<endl;
}
else
{for(int i=top; i>=0; i--)
{
cout<<"Stack["<<i<<"] : "<<a[i]<<endl;
}
}
}
int main()
{
int select;
char again;
do{
cout<<"***** 1.PUSH ****"<<endl;
cout<<"***** 2.POP ****"<<endl;
cout<<"***** 3.Display ****"<<endl;
cout<<"Select the option"<<endl;
cin>>select;
switch(select)
{
case 1:push();break;
case 2:pop(); break;
case 3:display();break;
default : cout<<"Wrong input"<<endl;
}
cout<<"Do You Want Again(Y/N)"<<endl;
cin>>again;
}while(again=='y'||again=='Y');
}
Output :
using namespace std;
#define max 5
int a[max];
int top=-1;
int input;
void push()
{
if(top==max-1)
{
cout<<"stack is overflow"<<endl;
}
else
{
cout<<"Enter the Element in the Stack"<<endl;
cin>>input;
top=top+1;
a[top]=input;
}
}
void pop()
{
if(top==-1)
{
cout<<"Stack is Empty"<<endl;
}
else
{
cout<<"Stack value : "<<a[top]<<endl;
top=top-1;
}
}
void display()
{
if(top==-1)
{
cout<<"Stack is Empty"<<endl;
}
else
{for(int i=top; i>=0; i--)
{
cout<<"Stack["<<i<<"] : "<<a[i]<<endl;
}
}
}
int main()
{
int select;
char again;
do{
cout<<"***** 1.PUSH ****"<<endl;
cout<<"***** 2.POP ****"<<endl;
cout<<"***** 3.Display ****"<<endl;
cout<<"Select the option"<<endl;
cin>>select;
switch(select)
{
case 1:push();break;
case 2:pop(); break;
case 3:display();break;
default : cout<<"Wrong input"<<endl;
}
cout<<"Do You Want Again(Y/N)"<<endl;
cin>>again;
}while(again=='y'||again=='Y');
}
Output :
Comments
Post a Comment