Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*Assume we need to store two stacks (Stk1 and Stk2) using physical locations Min

ID: 668897 • Letter: #

Question

*Assume we need to store two stacks (Stk1 and Stk2) using physical locations Min through Max.The size of Stk1 plus the size of Stk2 should never exceed (Max-Min) + 1 units of storage though we must check for overflow on insertion and underflow on deletion.At any given time, we must allow for either stack to occupy the entire memory (locations Min through Max) if the other stack is empty.Write the algorithms to insert (push) items in Stk1 and delete items from Stk2 subject to the above conditions.The stacks are considered empty any time TopI = BaseI.The initial condition reflecting empty stacks is shown in the diagram below.

Stk1

Stk2

Min-1

Min

Min+1

Max-1

Max

Max+1

T1

B1

T2

B2

Stk1

Stk2

Min-1

Min

Min+1

Max-1

Max

Max+1

T1

B1

T2

B2

Explanation / Answer

here are the two functions and you can also refer to the below link for detail explanation

http://www.geeksforgeeks.org/implement-two-stacks-in-an-array/

void push1(int x)

   {

       // There is at least one empty space for new element

       if (top1 < top2 - 1)

       {

           top1++;

           arr[top1] = x;

       }

       else

       {

           cout << "Stack Overflow";

           exit(1);

       }

   }

int pop2()

   {

       if (top2 < size)

       {

          int x = arr[top2];

          top2++;

          return x;

       }

       else

       {

           cout << "Stack UnderFlow";

           exit(1);

       }

   }