A program to implement Stacks in arrays. The program has four options as given bellow and it continues till the user wants
a) Push
b) Pop
c) Display
d) Exit

 

#include<iostream.h>
#include<conio.h>

int stack[10];
int top= -1;

void push()
{
if (top<9)
{
top++;
cout <<"\nEnter element: ";
cin>>stack[top];
cout<<"\nElement entered at location no."<<top+1<<endl;
}
else
cout<<"\nStack overflow."<<endl;
}

void pop()
{
if (top>-1)
{
cout <<"\nElement deleted: "<<stack[top]<<endl;
top--;
}
else
cout <<"\nStack underflow."<<endl;
}

void display()
{
clrscr();
int i;
cout<<"\nStack: ";
for (i=top;i>=0;i--)
cout <<stack[i]<<" ";
cout<<endl;
}

void main()
{
 clrscr();
 int ch;

 do
  {
   cout<<"1.Push."<<endl;
   cout<<"2.Pop."<<endl;
   cout<<"3.Display."<<endl;
   cout<<"4.Exit."<<endl;
   cout <<"Enter your choice: ";
   cin >>ch;
   switch(ch)
    {
     case 1: {
                  push();
                  break;
                 }
     case 2: {
                  pop();
                  break;
                 }
     case 3: {
                  display();
                  break;
                 }
     case 4: {
                  break;
                 }
     default: {
                  cout <<"\nWrong choice entered."<<endl;
                 }
    }
   cout<<endl;
  }
 while(ch!=4);
}

 

< Back