Friday, 7 December 2012

Stack operations cpp program

Program to implement the stack as an array

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

class IntStack
{ private:
          int *stackarr,stacksize,top;
  public:
          IntStack(int);
          void push(int);
          void pop(void);
          void display(void);
          int isfull();
          int isempty();
          int gettop()
          {return(top);}
};
IntStack:: IntStack(int size)
{        stackarr=new int[size];
          stacksize=size;
           top=-1;
}

void IntStack::push(int num)
{        if(isfull())
                   cout<<"\nThe stack is full.Can`t  push more elements";
          else
          {        top++;
                    stackarr[top]=num;
          }
          return;
}

void IntStack::pop(void)
{        if(isempty())
                   cout<<"\n The stack is empty .No elements to pop";
          else
          {        cout<<stackarr[top];
                    top--;
          }
  return;
}

void IntStack::display(void)
{        for(int i=top;i>-1;i--)
                   cout<<stackarr[i]<<" ";
          return;
}

int IntStack::isfull()
{        if(top==(stacksize-1))
                   return 1;
          else
                   return 0;
}

int IntStack::isempty()
{        if(top==-1)
                   return 1;
          else
                   return 0;
}

void main()
{        int c,num;
          char ch;
          IntStack stack(20);
          while(1)
          {        clrscr();
                   cout<<"\nPerform the following stack operations";
                   cout<<"\n-------------------------------";
                   cout<<"\n1.Push elements";
                   cout<<"\n2.Pop elements";
                   cout<<"\n3.Display full stack content";
                   cout<<"\n4.Exit from the program";
                   do
                    {        cout<<"\nSelect your choice(1:4)";
                             cin>>c;
                             cout<<"";
                    }
                   while((c<1)||(c>4));
          if(c==4)exit(1);
          clrscr();
          switch(c)
          { case 1 :    
                   do
{        cout<<"Stack currently has " <<stack.gettop()+1<<" elements\n";
                             cout<<"Enter the new element : ";
                             cin>>num;
                             stack.push(num);
                             cout<<"Push more(Y/N) : ";
                             cin>>ch;
                   }
                   while((ch=='y')||(ch=='Y'));
                   break;
          case 2 :
                   do
{        cout<<"Stack currently has " <<stack.gettop()+1<<" elements\n";
                             if(stack.gettop()!=-1) cout<<"The poped element is : ";
                             stack.pop();
                             cout<<"\nPop more (Y/N) : ";
                             cin>>ch;
                   }
                   while((ch=='y')||(ch=='Y'));
                   break;
          case 3 :
cout<<"Stack currently has " <<stack.gettop()+1<<" elements as shown below \n";
                   stack.display();
                   break;
          }
          cout<<"\nPress any key to continue............";
          getch();
      }
}       

 

No comments:

Post a Comment