Amazon Affiliate

Thursday 31 May 2012

UPDATING A STACK IMPLEMENTED WITH THE HELP OF ARRAYS..

// UPDATING A STACK IMPLEMENTED WITH THE HELP OF ARRAYS.


 # include<iostream.h>
 # include<stdio.h>
 # include<string.h>
 # include<ctype.h>

 # define size  100

    int top = -1;
    int flag = 0;

    class stacks
     {
       private: int stack[100];
            int data;

       public:
           void push (int *, int);
           int update (int *);
           void display (int *);
      };

// definition of the push function

    void stacks :: push(int s[], int d)
         {
          if(top ==(size-1))
         flag = 0;
         else
         {
         flag = 1;
         ++top;
         s[top] = d;

         }
         }

// definition of the pop function

    int stacks :: update(int s[])
         {
           int i;
           cout<<"\n Input the information number to which you want to update:";
           cin>> i;
           int update_element;
          if(top - i + 1 < 0)
             {
            update_element = 0;
            flag = 0;
             }
          else
           {
             flag = 1;
             update_element = s[top-i +1];
             cout<<"\n Input the new value:";
             cin>> s[top-i+1];
           }
           return (update_element);
        }

// definition of the display function

    void stacks :: display(int s[])
        {
           if(top == -1)
          {
            cout<<"Stack is empty";
          }
           else
         {
           for(int i = top; i>=0; --i)
           cout<<"  "<<s[i];
         }
          }

          void main()
           {
         stacks s;
         int stack[size];
         int  data;
         char choice;
         int q = 0;
         int top = -1;

         do
           {
             cout<<" \nPush->i Update->u Quit->q:";
             cout<<"\nInput the choice : ";
              do
              {
            cin>>choice;
            choice =tolower(choice);
               }while(strchr("iuq",choice)==NULL);
            cout<<"Your choice is ->"<<choice;

      switch(choice)
       {
     case 'i' :
           cout<<"\n Input the element to pushed:";
           cin>>data;
           s.push(stack,data);
           if(flag)
           {
           cout<<"\n After inserting ";
           s.display(stack);
           if(top == (size-1))
            cout<<"\n Stack is full";
           }
           else
           cout<<"\n Stack overflow after pushing";
           break;

    case 'u' : data = s.update(stack);
          if(flag)
            {
              cout<<"\n Data is peepped:"<<data;
              cout<<"\n Stack is as follows:\n";

              s.display(stack);
            }
            else
            cout<<"\n Stack underflow";
            break;
    case 'q': q = 1;
      }
    } while(!q);
   }

No comments:

Post a Comment