#include<iostream.h>
#include<conio.h>
#include<process.h>
const int max=5;
class stack
 {
  int s[max];
  int top;
  int temp;
  public:
  stack (int j,int p){top=j;temp=p;}
  void push(int item);
  void pop();
  void display();
 };
  void stack::push(int item)
   {
    if(top<4)
     {
      top++;
      s[top]=item;
     }
    else
     {
      cout<<"\nStack is full!";
     }
  }
  void stack::pop()
   {
    if(top>-1)
     {
      cout<<"\nThe popped item is "<<s[top];
      top--;
     }
    else
     {
      cout<<"\nStack is empty!";
     }
  }
 void stack::display()
  {
   temp=top;
   if(temp==-1)
    {
     cout<<"\nStack is empty!" ;
    }
    else
     {
      while(temp>=0)
       {
    cout<<"\n"<<s[temp];
    temp--;
       }
      }
  }
 void main()
  {
   clrscr();
   stack x(-1,-1);
   for(;;)
    {
     int ch,item;
     cout<<"\nEnter->\n1:push 2:pop 3:display 4:exit\t";
     cin>>ch;
     switch(ch)
      {
       case 1: cout<<"\nEnter the items\t:";
           cin>>item;
           x.push(item);
           break;
       case 2: x.pop();
           break;
       case 3: x.display();
           break;
       case 4: exit(0);
           break;
     }
   }
 }

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.