Monday 17 September 2012

WAP to implement push & pop operation on stack

Hello friends.................................
I am here with DSPM program. I going you to tell how to implement push & pop operations on Stack using linera array in C++.




//To implement push operation on the stack (using linear array)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
  int stack[10],maxstk,n,item,ch,top,i;
  cout<<"\n No. elements you want to enter: ";
  cin>>n;
  if(n!=0)
  {            cout<<"\nEnter the elements: ";
                for(i=0;i<n;i++)
                {  cin>>stack[i]; }   }
  maxstk=9;
  top=n-1;

     if(top==maxstk)
                                   { cout<<"\nOVERFLOW ! !"; }
                                   else
                                { cout<<"\nEnter the value you want to push : ";
                                cin>>item;
                                top=top+1;
                                stack[top]=item;
                                cout<<"\nAfter the push operation: ";
                                for(i=0;i<=top;i++)
                                {
        cout<<stack[i]<<"\t";
      }  }

getch();
  }



  //To implement pop operation on stack using Linear array
#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int stack[10],maxstk,n,item,ch,top,i;

  cout<<"\nNo. of  elements you want to enter: ";
  cin>>n;
  if(n!=0)
  {
   cout<<"\nEnter the elements: ";
                for(i=0;i<n;i++)
                {
     cin>>stack[i];
     }
      }
  maxstk=9;
  top=n-1;

                                if(top==-1)
                                {
         cout<<"\nUNDERFLOW ! !"; }
                                else
                                {
        cout<<"\nAfter the pop operation: ";
                                top=top-1;
                                for(i=0;i<=top;i++)
                                {
         cout<<stack[i]<<"\t";
          }
          }

   getch();
    }







1 comment :

Related Posts Plugin for WordPress, Blogger...