Growing direction of two stacks implementing similar array - Data Structure

Q.  What should be the growing direction of two stacks while implementing them in a similar array so as to reduce the overflow chances?
- Published on 28 Aug 15

a. Forward
b. Backward
c. Opposite
d. None of the above

ANSWER: Opposite
 

    Discussion

  • Nirja Shah   -Posted on 18 Nov 15
    If we implement two stacks in opposite direction with same array, then overflow chance will be reduce.
    Example:
    Suppose that array name is arr [ ]. We will consider the same array as two stack, stack1 and stack2. stack1 starts from the leftmost element, and stack2 starts from the rightmost corner. The first element in stack1 is pushed at index 0 and the first element in stack2 is pushed at index (n-1).
    class twoStacksTest
    {
        int ∗arr;
        int size;
        int top1, top2;
        int item, i,j;
    public:
       twoStacksTest() // constructor
    {
        cout << "\nEnter the size of stack" << endl;
        cin >>size;
        arr = new int[size];
        top1 = -1;
        top2 = size;
    }

    // Method to push an element to stack1
    void push1()
    {
        if (top1 < top2 - 1)
      {
        cout << "\nEnter the itemt\t";
         cin >>item;
         top1++;
         arr[top1]=item;

      }
    else
    {
         cout << "Stack Overflow\n\n";

    }
    }

    // Method to push an element to stack2
    void push2()
    {

    if (top1 < top2 - 1)
     {
         cout <<"Enter the itemt";
         cin >>item;
         top2--;
         arr[top2] = item;
     }
    else
     {
         cout << "Stack Overflow\n\n";
      }
    }

    // Method to pop an element from first stack
    void pop1()
      {
         if (top1 >= 0 )
      {
         cout <<"The deleted item is\t"<<arr[top1];
         top1--;
      }
    else
       {
         cout << "Stack UnderFlow\n\n";

       }
     }

    // Method to pop an element from second stack
    void pop2()
    {
      if (top2 < size)
     {
         cout <<"The deleted item is\t" <<arr[top2];
         top2++;

      }
    else
     {
        cout << "Stack UnderFlow\n\n";

      }
    }
    void display()
      {
        for(i=top1;i>=0;i--)
       {
         cout <<arr[i]<<"\t\t";
       }
         for(i=top2;i<size;i++)
       {
         cout<<arr[i]<<"\t\t";
       }
      }
    };
    int main()
      {

         twoStacksTest obj;
         int choice;
      do
        {
         cout <<"Enter 1. for Push1\n";
         cout <<"Enter 2. for Push2\n";
         cout <<"Enter 3. for Pop1\n";
         cout <<"Enter 4. for Pop2\n";
         cout <<"Enter 5. for Display\n";
         cout <<"Enter 6. for Exit\n";
         cout <<"Enter you choice\n";
         cin >>choice;
         switch(choice)
       {
        case 1:
          obj.push1();
          break;
         case 2:
          obj.push2();
          break;
         case 3:
          obj.pop1();
          break;
         case 4:
          obj.pop2();
          break;
         case 5:
          obj.display();
          break;
         case 6:
          break;
          default:
          cout<<"Wrong choice\n";
           break;

       }
    }
          while(choice <6);
          return 0;
       }

Post your comment / Share knowledge


Enter the code shown above:
 
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)