Friday, August 31, 2012

C Program for Stack Push POP Operations

/*Push and Pop Operation on Stack*/
#include<iostream.h>
#include<process.h>
#include<conio.h>
class stack
{
  char ch;
  int x[10],i,TOP;
  public:
  stack()
  { TOP=0;}
  void push(int);
  int pop();
  void process();
};
void stack::push(int y)
{
  if (TOP>=10)
  {
  cout<<"SORRY! OVERFLOW ERROR......";
  exit(0);
  }
 x[++TOP]=y;
}
int stack::pop()
{
  if (TOP<1)
  {
  cout<<"SORRY! UNDERFLOW ERROR......";
  exit(0);
  }
  return(x[TOP--]);
}
void stack::process()
{
 while(1)
 {
//   clrscr();
   cout<<"WHAT WOULD YOU LIKE TO DO?\n";
   cout<<"1.PUSH \t 2.POP \t3.EXIT";
   ch=getche();
   switch(ch)
   {
    case '1':
       cout<<"\nEnter value.:";
       cin>>i;
       push(i);
      cout<<"\nVALUE IN STACK.:";
      for(i=1;i<=TOP;i++)
      cout<<"  "<<x[i];
       break;
    case '2':
      i=pop();
      cout<<"\nVALUE POPED.:"<<i;
      cout<<"\nVALUE IN STACK.:";
      for(i=1;i<=TOP;i++)
      cout<<"  "<<x[i];
      break;
    case '3':
      exit(0);
    default:
      cout<<"SORRY! Invalid Input";
   }
   getch();
 }
}
void main()
{
  clrscr();
  stack s;
  s.process();
  getch();
}

No comments:

Post a Comment