Stack practical (Main function)
int stack[100],top,n,choice,x;
void push();
void pop();
void display();
int main()
{
top=-1;
cout<<"\n\t enter the size of the stack max[100] ";
cin>>n;
cout<<"\n\t\stack operation using arrays";
cout<<"\n\t\ 1.push \n\t\ 2.pop \n\t\ 3. display \n\t\ 4. exit";
do{
cout<<"\n\t enter your choice ";
cin>>choice;
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
cout<<" \n\t exiting from stack";
break;
default:
cout<<"\n\t please enter valid choice (1/2/3/4) ";
}
}
while(choice!=4);
return 0;
}
Push function
void push()
{
if(top>=n-1)
{
cout<<" \n\t stack is overflow";
}
else
{
cout<<"\n\t enter new element";
cin>>x;
top++;
stack[top]=x;
}
}
Pop function
void pop()
{
if(top<=-1)
{
cout<<" \n\t stack is underflow";
}
else
{
cout<<"\n\t this element is deleted"<<stack[top];
top--;
}
}
Display function
void display()
{
if(top>=0)
{
cout<<"\n\t the elements are ";
for(int i=top; i>=0; i--)
{
cout<<"\n\t "<<stack[i];
}
cout<<"\n\t press next choice";
}
else
{
cout<<"\n\t stack is empty"<<endl;
}
}