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 n n n stacks.
Two stacks in one array
Intuition
Two people share one bookshelf. One fills it from the left end, the other from the right end. Neither runs out of space until the two rows of books meet in the middle, however unevenly they are filled.
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 n n n cells (indices 0 to n − 1 n - 1 n − 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 n n n cells are used, no matter how they are divided between A and B.
Example 4.7 Two stacks sharing 8 cells
Goal. Put 6 items on A and 1 on B, then keep pushing and see when overflow occurs.
Program.
C · two_stacks.c Copy #include <stdio.h>
#define N 8 /* one array shared by two stacks */
int x[N];
int topA = - 1 ; /* stack A grows to the right from x[0] */
int topB = N; /* stack B grows to the left from x[N-1] */
/* Overflow only when the two tops meet: no free cell between them. */
int pushA( int v) { if (topA + 1 == topB) return 0 ; x[ ++ topA] = v; return 1 ; }
int pushB( int v) { if (topB - 1 == topA) return 0 ; x[ -- topB] = v; return 1 ; }
int popA( int * v) { if (topA == - 1 ) return 0 ; * v = x[topA -- ]; return 1 ; }
int popB( int * v) { if (topB == N) return 0 ; * v = x[topB ++ ]; return 1 ; }
void show( void )
{
printf( "A:" );
for ( int i = 0 ; i <= topA; i ++ )
printf( " %d" , x[i]);
printf( " | free: %d | B:" , topB - topA - 1 );
for ( int i = N - 1 ; i >= topB; i -- )
printf( " %d" , x[i]);
printf( " \n " );
}
int main( void )
{
int v;
for ( int i = 1 ; i <= 6 ; i ++ ) /* A takes 6 of the 8 cells */
pushA(i * 10 );
pushB( 100 );
show();
printf( "pushB(200): %s \n " , pushB( 200 ) ? "ok" : "overflow" );
show();
printf( "pushA(70): %s \n " , pushA( 70 ) ? "ok" : "overflow" );
popA( & v);
printf( "popA -> %d \n " , v);
printf( "pushB(300): %s \n " , pushB( 300 ) ? "ok" : "overflow" );
show();
return 0 ;
}
Run it.
Output Copy A: 10 20 30 40 50 60 | free: 1 | B: 100
pushB(200): ok
A: 10 20 30 40 50 60 | free: 0 | B: 100 200
pushA(70): overflow
popA -> 60
pushB(300): ok
A: 10 20 30 40 50 | free: 0 | B: 100 200 300
Result. B could take 2 items even though A had used 6 of the 8 cells; overflow occurred only when no free cell was left, and a pop from A immediately made room for B.
More than two stacks
An array has only two ends, so for n > 2 n > 2 n > 2 stacks we divide it into n n n segments, one per stack. Following Horowitz and Sahni (as §4.4 does), for each stack i i i :
B ( i ) B(i) B ( i ) is one position below the bottom of stack i i i ;
T ( i ) T(i) T ( i ) is the position of the top item of stack i i i ;
stack i i i is empty when T ( i ) = B ( i ) T(i) = B(i) T ( i ) = B ( i ) .
With an array X ( 1 : m ) X(1:m) X ( 1 : m ) split into n n n roughly equal segments, the initial values are
B ( i ) = T ( i ) = ⌊ m n ⌋ ( i − 1 ) , 1 ≤ i ≤ n , B(i) = T(i) = \left\lfloor \frac{m}{n} \right\rfloor (i - 1), \quad 1 \le i \le n, B ( i ) = T ( i ) = ⌊ n m ⌋ ( i − 1 ) , 1 ≤ i ≤ n ,
and an extra boundary B ( n + 1 ) = m B(n + 1) = m B ( n + 1 ) = m marks the end of the last segment.
Definition 4.8 · ADD(i, e): push e onto stack i
If T ( i ) = B ( i + 1 ) T(i) = B(i + 1) T ( i ) = B ( i + 1 ) , stack i i i has filled its segment: report “stack i i i full” and stop.
T ( i ) ← T ( i ) + 1 T(i) \leftarrow T(i) + 1 T ( i ) ← T ( i ) + 1 .
X ( T ( i ) ) ← e X(T(i)) \leftarrow e X ( T ( i )) ← e .
Definition 4.9 · DELETE(i, e): pop stack i into e
If T ( i ) = B ( i ) T(i) = B(i) T ( i ) = B ( i ) , report “stack i i i empty” and stop.
e ← X ( T ( i ) ) e \leftarrow X(T(i)) e ← X ( T ( i )) .
T ( i ) ← T ( i ) − 1 T(i) \leftarrow T(i) - 1 T ( i ) ← T ( i ) − 1 .
Correction · Index ranges in the multiple-stack scheme
Study material says: §4.4 writes the conditions as “TM(i), 1 < i < n” and “BM (i) = TM (i) = m/n (i – 1), 1 < i < n”, and the ADD algorithm tests TM(i) = BM(i+1) without defining B M ( n + 1 ) BM(n+1) B M ( n + 1 ) .
Correct version: The index runs over all stacks: 1 ≤ i ≤ n 1 \le i \le n 1 ≤ i ≤ n . The initial values are B ( i ) = T ( i ) = ⌊ m / n ⌋ ( i − 1 ) B(i) = T(i) = \lfloor m/n \rfloor (i - 1) B ( i ) = T ( i ) = ⌊ m / n ⌋ ( i − 1 ) for 1 ≤ i ≤ n 1 \le i \le n 1 ≤ i ≤ n , and B ( n + 1 ) = m B(n + 1) = m B ( n + 1 ) = m must also be set so that the full test works for the last stack.
Why: With strict inequalities, stacks 1 and n n n would be excluded. For i = n i = n i = n , ADD compares T ( n ) T(n) T ( n ) with B ( n + 1 ) B(n + 1) B ( n + 1 ) ; if B ( n + 1 ) B(n + 1) B ( n + 1 ) is not defined, the last stack has no upper limit. Horowitz, Sahni and Mehta, Fundamentals of Data Structures , use 1 ≤ i ≤ n 1 \le i \le n 1 ≤ i ≤ n and set B ( n + 1 ) = m B(n + 1) = m B ( n + 1 ) = m .
Example 4.10 Three stacks in a 12-cell array
Goal. Use m = 12 m = 12 m = 12 cells for n = 3 n = 3 n = 3 stacks (0-based C indices, so every boundary is shifted down by 1), fill stack 0, and use stacks 1 and 2.
Initial values. ⌊ 12 / 3 ⌋ = 4 \lfloor 12/3 \rfloor = 4 ⌊ 12/3 ⌋ = 4 : B = T = − 1 , 3 , 7 B = T = -1, 3, 7 B = T = − 1 , 3 , 7 for stacks 0, 1, 2, and B ( 3 ) = 11 B(3) = 11 B ( 3 ) = 11 .
C · multi_stack.c Copy #include <stdio.h>
#define M 12 /* size of the shared array X[0..M-1] */
#define NS 3 /* number of stacks */
int X[M];
int B[NS + 1 ]; /* B[i] = index one below the bottom of stack i; B[NS] = M - 1 */
int T[NS]; /* T[i] = index of the top of stack i; T[i] == B[i] means empty */
void init( void )
{
for ( int i = 0 ; i < NS; i ++ )
B[i] = T[i] = (M / NS) * i - 1 ; /* equal segments */
B[NS] = M - 1 ; /* boundary after the last stack */
}
int add( int i, int e) /* push e onto stack i */
{
if (T[i] == B[i + 1 ])
return 0 ; /* stack i has filled its segment */
X[ ++ T[i]] = e;
return 1 ;
}
int delete_top( int i, int * e) /* pop stack i into *e */
{
if (T[i] == B[i])
return 0 ; /* stack i is empty */
* e = X[T[i] -- ];
return 1 ;
}
void show( void )
{
for ( int i = 0 ; i < NS; i ++ ) {
printf( "stack %d (cells %d..%d):" , i, B[i] + 1 , B[i + 1 ]);
for ( int k = B[i] + 1 ; k <= T[i]; k ++ )
printf( " %d" , X[k]);
printf( " \n " );
}
}
int main( void )
{
int e;
init();
for ( int i = 0 ; i < NS; i ++ )
printf( "B[%d] = T[%d] = %d \n " , i, i, B[i]);
printf( "B[%d] = %d \n " , NS, B[NS]);
for ( int v = 1 ; v <= 5 ; v ++ )
printf( "add(0, %d): %s \n " , v, add( 0 , v) ? "ok" : "stack 0 is full" );
add( 1 , 50 );
add( 2 , 90 );
add( 2 , 91 );
show();
delete_top( 2 , & e);
printf( "delete from stack 2 -> %d \n " , e);
printf( "delete from stack 1 -> %s \n " , delete_top( 1 , & e) ? "ok" : "empty" );
printf( "delete from stack 1 -> %s \n " , delete_top( 1 , & e) ? "ok" : "empty" );
show();
return 0 ;
}
Run it.
Output Copy B[0] = T[0] = -1
B[1] = T[1] = 3
B[2] = T[2] = 7
B[3] = 11
add(0, 1): ok
add(0, 2): ok
add(0, 3): ok
add(0, 4): ok
add(0, 5): stack 0 is full
stack 0 (cells 0..3): 1 2 3 4
stack 1 (cells 4..7): 50
stack 2 (cells 8..11): 90 91
delete from stack 2 -> 91
delete from stack 1 -> ok
delete from stack 1 -> empty
stack 0 (cells 0..3): 1 2 3 4
stack 1 (cells 4..7):
stack 2 (cells 8..11): 90
Result. Stack 0 reports “full” after 4 items although stacks 1 and 2 still have free cells; each stack is confined to its own 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 i i i 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 idea
Two stacks can share one array perfectly by growing from opposite ends (overflow only when the array is full); n > 2 n > 2 n > 2 stacks need a segment each, with boundaries B ( i ) B(i) B ( i ) and tops T ( i ) T(i) T ( i ) , and may report “full” while other segments still have room.
TEE Exam note
For “implement two stacks in one array” draw the array with A growing right and B growing left, give the initial tops (−1 and n n n ), the push/pop code for both, and the overflow condition topA + 1 == topB. For n n n stacks, give the initial formula for B ( i ) B(i) B ( i ) and T ( i ) T(i) T ( i ) , the boundary B ( n + 1 ) = m B(n + 1) = m B ( n + 1 ) = m , the ADD and DELETE algorithms, and draw the initial configuration. Check Your Progress 2, Q1: multiple stacks can be implemented using arrays (or linked lists, one per stack).
Common mistake
Testing overflow for two stacks with topA == n / 2. That wastes the free half of the other stack; the stacks must be allowed to meet anywhere.
Common mistake
Using T ( i ) = B ( i ) T(i) = B(i) T ( i ) = B ( i ) as the full test. T ( i ) = B ( i ) T(i) = B(i) T ( i ) = B ( i ) means empty; full is T ( i ) = B ( i + 1 ) T(i) = B(i + 1) T ( i ) = B ( i + 1 ) .
Key takeaways
Two stacks: grow from opposite ends; full only when topA + 1 == topB.
n n n stacks: segment i i i lies between B ( i ) B(i) B ( i ) and B ( i + 1 ) B(i + 1) B ( i + 1 ) ; empty when T ( i ) = B ( i ) T(i) = B(i) T ( i ) = B ( i ) , full when T ( i ) = B ( i + 1 ) 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) B ( i ) = T ( i ) = ⌊ m / n ⌋ ( i − 1 ) , and B ( n + 1 ) = m B(n + 1) = m B ( 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?
A When topA == 4 B When topA == 9 C When topA + 1 == topB D When topB == 10
Check answer
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)?
A 1 B 0 C 3 D 8
Check answer
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) X ( 1 : 20 ) is shared by n = 4 n = 4 n = 4 stacks in equal segments. What is the initial value of B ( 3 ) = T ( 3 ) B(3) = T(3) B ( 3 ) = T ( 3 ) ?
A 15 B 5 C 10 D 12
Check answer
Show answer Answer: C. 10
⌊ 20 / 4 ⌋ ( 3 − 1 ) = 5 × 2 = 10 \lfloor 20/4 \rfloor (3 - 1) = 5 \times 2 = 10 ⌊ 20/4 ⌋ ( 3 − 1 ) = 5 × 2 = 10 . Stack 3 then uses cells 11 to 15.
Question 4 · multiple choice In the n n n -stack scheme, which condition means stack i i i is empty?
Check answer
Show answer Answer: B. T ( i ) = B ( i ) T(i) = B(i) T ( i ) = B ( i )
B ( i ) B(i) B ( i ) is one below the bottom of stack i i i , so the top equals it only when there are no items. T ( i ) = B ( i + 1 ) 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 n n n stacks?
Check answer
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 i i i -th of n n n stacks stored in an array X ( 1 : m ) 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) B ( i ) = T ( i ) = ⌊ m / n ⌋ ( i − 1 ) for 1 ≤ i ≤ n 1 \le i \le n 1 ≤ i ≤ n , and B ( n + 1 ) = m B(n + 1) = m B ( n + 1 ) = m . ADD(i, e): (1) if T ( i ) = B ( i + 1 ) T(i) = B(i + 1) T ( i ) = B ( i + 1 ) , print “stack i is full” and exit; (2) T ( i ) ← T ( i ) + 1 T(i) \leftarrow T(i) + 1 T ( i ) ← T ( i ) + 1 ; (3) X ( T ( i ) ) ← e X(T(i)) \leftarrow e X ( T ( i )) ← e . DELETE(i, e): (1) if T ( i ) = B ( i ) 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)) e ← X ( T ( i )) ; (3) T ( i ) ← T ( i ) − 1 T(i) \leftarrow T(i) - 1 T ( i ) ← T ( i ) − 1 . Both are O ( 1 ) O(1) O ( 1 ) .