1,420 views
1 votes
1 votes

Consider the implementation of multiple stacks in single array S of size P from index 0 to P – 1. Number of stack Q each of size P/Q

Now, how push() and pop() implemented in it. Can somebody give some insight on this implementation

1 Answer

0 votes
0 votes
Consider stack numbered as $0,1,2....(P/Q-1)$.

Size of each stack is fixed$( P/Q)$.  So, allocation of the stacks will be like--

Stack 0-- Index 0 to Index $(P/Q-1)$

Stack 1-- Index P to Index $(2*P/Q-1)$

....

....

Stack (P/Q-1) --- Index$( (P/Q-1)*P)$ to Index $(P/Q*P/Q-1)$

 

For a stack 'n'( n starting form 0 and ending at $(P/Q-1))$ the condition for top empty and full stack will change. Top  being as $top0, top1, top2... topN$. Initially before any push or pop the values of top being

$top0 = 0*P/Q-1$,

$top1= 1*P/Q-1$

...

$topN = (P/Q-1)*P/Q-1$

Stack operations will be something just like this.

$IsEmpty(Stack-n)${

$if( topN == (n*P/Q) -1 )$
        return Stack-n is Empty

}

$IsFull(Stack-n)${

$if( topN == (((n+1)*P/Q)-1 )$

     return Stack-n is Full

}

Related questions

0 votes
0 votes
3 answers
1
vivek1211 asked Nov 6, 2022
754 views
The maximum size of the operator stack when converting the following infix to postfix expressiona ^ b * c * d + e * f ^ g (assume that “^” has highest precedence and ...
1 votes
1 votes
1 answer
2
amit166 asked Jan 6, 2019
1,414 views
How many enqueue and dequeue operations are required to perform a pop operation if Q1 contains n element initially?
1 votes
1 votes
1 answer
3
himanshu6398 asked Oct 9, 2018
426 views
While calculating the cost of growable array-based stack.... the cost of n pushes came out as a series - 2 + 4 + 8 + 16 +......+2^(logn + 1) and it equals to 4n - 1. I di...