MCS-208 · Unit 4

Stacks

Section 4 of 7

Block 2 · Unit 4Stacks15 min · Section 4 of 7

4.4Multiple Stacks in One Array

Covers IGNOU MCS-208 §4.4

A program may need several stacks at the same time. Giving each its own fixed array wastes memory: one stack may overflow while another is almost empty. Letting stacks share one array uses the memory better. This page shows the neat solution for two stacks and the standard scheme for nn stacks.

Two stacks in one array

Stack A starts at the left end and grows to the right; stack B starts at the right end and grows to the left. With an array of nn cells (indices 0 to n−1n - 1):

  • topA = -1 and topB = n when both are empty;
  • push on A: x[++topA] = v; push on B: x[--topB] = v;
  • overflow happens only when topA + 1 == topB, that is when all nn cells are used, no matter how they are divided between A and B.

More than two stacks

An array has only two ends, so for n>2n > 2 stacks we divide it into nn segments, one per stack. Following Horowitz and Sahni (as §4.4 does), for each stack ii:

  • B(i)B(i) is one position below the bottom of stack ii;
  • T(i)T(i) is the position of the top item of stack ii;
  • stack ii is empty when T(i)=B(i)T(i) = B(i).

With an array X(1:m)X(1:m) split into nn roughly equal segments, the initial values are

B(i)=T(i)=⌊mn⌋(i−1),1≤i≤n,B(i) = T(i) = \left\lfloor \frac{m}{n} \right\rfloor (i - 1), \quad 1 \le i \le n,

and an extra boundary B(n+1)=mB(n + 1) = m marks the end of the last segment.

That last point is the weakness of fixed segments: a stack can be “full” while the array still has space. Better versions, when stack ii is full, look for free space in a neighbouring segment and shift the stacks between them to make room, at the cost of moving elements.

Key takeaways

  • Two stacks: grow from opposite ends; full only when topA + 1 == topB.
  • nn stacks: segment ii lies between B(i)B(i) and B(i+1)B(i + 1); empty when T(i)=B(i)T(i) = B(i), full when T(i)=B(i+1)T(i) = B(i + 1).
  • Initial values: B(i)=T(i)=⌊m/n⌋(i−1)B(i) = T(i) = \lfloor m/n \rfloor (i - 1), and B(n+1)=mB(n + 1) = m.
  • Fixed segments can waste space; shifting stacks can recover it at a cost.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Two stacks share an array of 10 cells, A from the left (topA starts at −1) and B from the right (topB starts at 10). When does pushing onto A overflow?

Show answer

Answer: C. When topA + 1 == topB

A may use any cell B has not taken; overflow occurs only when the next cell is B’s top. topA == 4 would split the array in half and waste space.

Question 2 · multiple choice

Two stacks share 10 cells. A holds 7 items and B holds 2. How many more items can be pushed (onto either stack)?

Show answer

Answer: A. 1

7 + 2 = 9 cells are used, so 1 cell is free, and either stack may take it.

Question 3 · multiple choice

An array X(1:20)X(1:20) is shared by n=4n = 4 stacks in equal segments. What is the initial value of B(3)=T(3)B(3) = T(3)?

Show answer

Answer: C. 10

⌊20/4⌋(3−1)=5×2=10\lfloor 20/4 \rfloor (3 - 1) = 5 \times 2 = 10. Stack 3 then uses cells 11 to 15.

Question 4 · multiple choice

In the nn-stack scheme, which condition means stack ii is empty?

Show answer

Answer: B. T(i)=B(i)T(i) = B(i)

B(i)B(i) is one below the bottom of stack ii, so the top equals it only when there are no items. T(i)=B(i+1)T(i) = B(i + 1) is the full condition.

Question 5 · multiple choice

What is the main disadvantage of dividing one array into fixed equal segments for nn stacks?

Show answer

Answer: D. One stack can be full while other segments still have free cells.

Each stack is limited to its segment. Recovering the unused space requires shifting stacks, which costs time.

Question 6 · short answer

Write the algorithms to push onto and pop from the ii-th of nn stacks stored in an array X(1:m)X(1:m), and give the initial values of the boundaries.

Show worked answer

Initialisation: B(i)=T(i)=⌊m/n⌋(i−1)B(i) = T(i) = \lfloor m/n \rfloor (i - 1) for 1≤i≤n1 \le i \le n, and B(n+1)=mB(n + 1) = m. ADD(i, e): (1) if T(i)=B(i+1)T(i) = B(i + 1), print “stack i is full” and exit; (2) T(i)←T(i)+1T(i) \leftarrow T(i) + 1; (3) X(T(i))←eX(T(i)) \leftarrow e. DELETE(i, e): (1) if T(i)=B(i)T(i) = B(i), print “stack i is empty” and exit; (2) e←X(T(i))e \leftarrow X(T(i)); (3) T(i)←T(i)−1T(i) \leftarrow T(i) - 1. Both are O(1)O(1).

↑ ↓ move↵ openTab switch type