Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • To Implement two stacks in an array in C++?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.54k
    Comment on it

    To Implement two stacks in an array

    #include<iostream>
    #include<stdlib.h>
     
    using namespace std;
     
    class twoStacks
    {
        int *a;
        int size;
        int top1, top2;     //top of two stacks
    public:
       twoStacks(int n)  // constructor
       {
           size = n;
           a = new int[n];
           top1 = -1;
           top2 = size;
       }
     
       void push1(int x)  //Method pushing an element to stack1
       {
           if (top1 < top2 - 1) //checking for an empty space for new element in Stack1
           {
               top1++;
               a[top1] = x;
           }
           else
           {
               cout << "Stack Overflow";
               exit(1);
           }
       }
     
       void push2(int x)  //Method pushing an element to stack2
       {
           if (top1 < top2 - 1)  //checking for an empty space for new element in Stack2
           {
               top2--;
               a[top2] = x;
           }
           else
           {
               cout << "Stack Overflow";
               exit(1);
           }
       }
     
       int pop1()    //Method to pop an element from stack1
       {
           if (top1 >= 0 )
           {
              int x = a[top1];
              top1--;
              return x;
           }
           else
           {
               cout << "Stack UnderFlow";
               exit(1);
           }
       }
     
       int pop2()   //Method to pop an element from stack2
       {
           if (top2 < size)
           {
              int x = a[top2];
              top2++;
              return x;
           }
           else
           {
               cout << "Stack UnderFlow";
               exit(1);
           }
       }
    };
     
     
    int main()
    {
        twoStacks st(5);
        st.push1(5);
        st.push2(10);
        st.push2(15);
        st.push1(11);
        st.push2(7);
        cout << "Popped element from stack1 is " << st.pop1();
        ts.push2(40);
        cout << "\nPopped element from stack2 is " << st.pop2();
        return 0;
    }

    Output:

    Popped element from stack1 is 11
    Popped element from stack2 is 40

    Explanation of above program:

    An array can contain information of two stacks. In the above program, two stacks push and pop operation are defined. Push operation to fill element in stacks and pop to retrieve element from stacks. Top of 2 stacks : top of Stack1 is -1 while top of Stack2 is size of array. Elements in Stack1 is filled in incremented manner  i.e top is incremented while in Stack2 top is decremented until top1 <top2-1. Since stack is Last in First Out datastructure in pop operation in Stack1 top is decremented while in Stack2 top is incremented. This is space efficient implementation of two Stacks in an array.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: