Chapter 8: Stack and Queue Applications
Introduction
Learning Outcomes
Search a maze with a stack and trace how it backs out of dead ends.
Design and analyze the worst-case input for an algorithm.
Convert among infix, prefix, and postfix by hand and with a stack.
Run eval step by step with its operand stack.
Write the infix → postfix routine using the isp/icp tables.
Use stacks for bracket matching, undo/redo, and non-recursive traversal.
Maze
The Maze Problem
In an m×p matrix, 0s are open paths and 1s are walls. The entrance is [1][1] (S) and the exit [6][6] (E): we want a path from entrance to exit.
- Moves are horizontal, vertical, and diagonal: 8 directions.
- To avoid checking “am I leaving the matrix?” at every step, the matrix is surrounded by a border of 1s.
- Visited cells are marked in the
markmatrix, so no cell is entered twice. - At a dead end we back up to the most recent fork: last in, first out → a stack.
Maze
Eight Directions and the move[] Table
| dir | move[dir].vert | move[dir].horiz |
|---|---|---|
| 0 | −1 | 0 |
| 1 | −1 | 1 |
| 2 | 0 | 1 |
| 3 | 1 | 1 |
| 4 | 1 | 0 |
| 5 | 1 | −1 |
| 6 | 0 | −1 |
| 7 | −1 | −1 |
Direction 0 is north; 1…7 go clockwise. New position: [i + vert][j + horiz].
ifs: the direction becomes a loop variable.Maze
Definitions for the Maze
#define MAX_STACK_SIZE 100 #define EXIT_ROW 6 #define EXIT_COL 6 typedef struct { short int vert, horiz; } offsets; offsets move[8] = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}}; typedef struct { short int row, col, dir; } element; element stack[MAX_STACK_SIZE]; int top = -1; short int mark[8][8]; short int maze[8][8] = {{1,1,1,1,1,1,1,1}, {1,0,1,0,0,0,1,1}, {1,0,1,0,1,1,1,1}, {1,1,0,1,1,1,1,1}, {1,0,1,1,0,1,1,1}, {1,1,0,0,1,0,1,1}, {1,1,1,1,1,1,0,1}, {1,1,1,1,1,1,1,1}};
element: the record pushed on the stack: which cell (row, col) and, if we come back, which direction to try next (dir).mark: visited cells (zero at the start because it is global).maze: an 8×8 matrix with a border of 1s; the interior is 6×6.- The stack never holds more entries than there are open cells (13 here).
Maze
The path Routine
void path(void) { short int row, col, next_row, next_col; int i, dir, found = FALSE; element position; stack[0].row = stack[0].col = 1; stack[0].dir = 1; /* push the start */ mark[1][1] = 1; top = 0; while (top > -1 && !found) { position = pop(&top); /* next one */ row = position.row; col = position.col; dir = position.dir; /* which way */ while (dir < 8 && !found) { next_row = row + move[dir].vert; next_col = col + move[dir].horiz;
if (next_row == EXIT_ROW && next_col == EXIT_COL) found = TRUE; /* the exit? */ else if (!maze[next_row][next_col] && !mark[next_row][next_col]) { mark[next_row][next_col] = 1; position.row = row; /* came from */ position.col = col; position.dir = ++dir; /* resume dir */ push(&top, position); row = next_row; col = next_col; dir = 0; } else ++dir; /* try another */ } /* back up */ }
If found, the stack positions (0…top), the last position and the exit are printed; otherwise “The maze does not have a path”. (Lines split for readability; logic unchanged.)
Maze · Run the code step by step
Code Trace: Backtracking with a Stack
Start: the stack holds (1,1) dir 1. stack: row,col,dir records (bottom to top).
First pop: (1,1), starting at dir 1 (dir 0 is a wall anyway).
From (1,1), dir 4 (S) is open → move to (2,1); (1,1, dir 5) pushed.
From (2,1), dir 3 (SE) is open → move to (3,2); (2,1, dir 4) pushed.
From (3,2), dir 1 (NE) is open → move to (2,3); (3,2, dir 2) pushed.
From (2,3), dir 0 (N) is open → move to (1,3); (2,3, dir 1) pushed.
From (1,3), dir 2 (E) is open → move to (1,4); (1,3, dir 3) pushed.
From (1,4), dir 2 (E) is open → move to (1,5); (1,4, dir 3) pushed.
Back up / pop: return to (1,4) and keep trying from dir 3 (SE).
Back up / pop: return to (1,3) and keep trying from dir 3 (SE).
Back up / pop: return to (2,3) and keep trying from dir 1 (NE).
Back up / pop: return to (3,2) and keep trying from dir 2 (E).
From (3,2), dir 5 (SW) is open → move to (4,1); (3,2, dir 6) pushed.
From (4,1), dir 3 (SE) is open → move to (5,2); (4,1, dir 4) pushed.
From (5,2), dir 2 (E) is open → move to (5,3); (5,2, dir 3) pushed.
From (5,3), dir 1 (NE) is open → move to (4,4); (5,3, dir 2) pushed.
From (4,4), dir 3 (SE) is open → move to (5,5); (4,4, dir 4) pushed.
Dir 3 (SE) is the exit! Path: the stack + (5,5) + (6,6).
while (top > -1 && !found) { position = pop(&top); row = position.row; col = position.col; dir = position.dir; while (dir < 8 && !found) { nr = row + move[dir].vert; nc = col + move[dir].horiz; if (nr == EXIT_ROW && nc == EXIT_COL) found = TRUE; else if (!maze[nr][nc] && !mark[nr][nc]) { mark[nr][nc] = 1; position = (element){row, col, ++dir}; push(&top, position); row = nr; col = nc; dir = 0; } else ++dir; } }
Maze
Assessment and the Worst Case
- The eight-entry look-up table shortened the code and made the job easier.
- The program does a brute-force search of the maze with a stack.
- A worst-case analysis needs the worst-case input; the order in which directions are scanned decides it.
- Input 1: the first path tried is long but correct → only pushes, no pops.
- Input 2: the first path tried is long and wrong → it is followed to the end and the stack is emptied one by one; the solution is reached only after every cell off the correct path has been visited. This is the worst-case input.
Maze
Complexity, and a Comparison with a Queue
Each open cell is pushed at most once (mark), and at most 8 directions are tried per cell: O(m·p) for an m×p maze.
The stack grows at most to the number of open cells: O(m·p); mark is O(m·p) too.
The path found need not be the shortest. In this maze it went into the (2,3)→(1,3)→(1,4)→(1,5) dead end and came back; the path it found has 9 cells.
Expands cells in order of distance, so it guarantees the shortest path. Here it finds the same 9-cell path, but in general the DFS path can be longer.
Expression Notations
Evaluating Expressions
Compilers also use stacks to evaluate logical and arithmetic expressions. How does a compiler cope with expressions like these?
x = a / b − c + d * e − a * c
((4 / 2) − 2) + (3 * 3) − (4 * 2) = 1
a / (b − c) + d * (e − a) * c
- Reading left to right, we can't apply an operator right away: a higher-precedence operator may follow it.
- The solution has two phases: first convert the expression to postfix according to precedence, then evaluate the postfix in a single pass. Both use a stack.
Expression Notations
Operator Precedence in C (Summary)
| Precedence | Operators | Associativity |
|---|---|---|
| highest | () [] -> . | left to right |
unary − ! ++ -- * & | right to left | |
* / % | left to right | |
+ − | left to right | |
< <= > >= | left to right | |
== != | left to right | |
&& | left to right | |
| lowest | || | left to right |
- Precedence comes first; on a tie, associativity decides:
a − b − c=(a − b) − c. - Parentheses override everything.
- Example:
a / b − c + d * e − a * c=(((a / b) − c) + (d * e)) − (a * c). - The algorithms in this chapter handle only
+ − * / %and parentheses (Horowitz–Sahni, Figure 3.12).
Expression Notations
Infix, Prefix, Postfix
- Infix: the operator sits between its operands,
a + b. Both parentheses and precedence must be taken into account. - Prefix (Polish notation): the operator comes first,
+ a b. - Postfix (reverse Polish): the operator comes last,
a b +. Prefix and postfix need no parentheses and are evaluated without thinking about precedence.
| # | Prefix | Infix | Postfix |
|---|---|---|---|
| 1 | *34 | 3*4 | 34* |
| 2 | +2*34 | 2+3*4 | 234*+ |
| 3 | -*+abc/da | (a+b)*c-d/a | ab+c*da/- |
| 4 | **/a+-bcd-eac | (a/(b-c+d))*(e-a)*c | abc-d+/ea-*c* |
Expression Notations
Converting by Hand: Parenthesize, Move, Erase
a / b − c + d * e − a * cThe starting expression.
((((a / b) − c) + (d * e)) − (a * c))1. Fully parenthesize by precedence and associativity.
((((a b /) c −) (d e *) +) (a c *) −)2. Move each operator to the right parenthesis that closes it.
a b / c − d e * + a c * −3. Erase the parentheses: postfix. (For prefix, move each operator to its left parenthesis: − + − / a b c * d e * a c.)
Expression Notations
Postfix → Infix: 42/2−33*+42*−
4 operand → push.
2 operand → push.
/ operator → pop two (4, 2) and push back (4/2).
2 operand → push.
- operator → pop two ((4/2), 2) and push back ((4/2)-2).
3 operand → push.
3 operand → push.
* operator → pop two (3, 3) and push back (3*3).
+ operator → pop two (((4/2)-2), (3*3)) and push back (((4/2)-2)+(3*3)).
4 operand → push.
2 operand → push.
* operator → pop two (4, 2) and push back (4*2).
- operator → pop two ((((4/2)-2)+(3*3)), (4*2)) and push back ((((4/2)-2)+(3*3))-(4*2)). Result: ((((4/2)−2)+(3*3))−(4*2)).
After the *, cell [1] is not empty: it holds (3*3), then (4*2) (top = 1).
Postfix Evaluation
Tokens: get_token
typedef enum { lparen, rparen, plus, minus, times, divide, mod, eos, operand } precedence; precedence get_token(const char *expr, char *symbol, int *n) { *symbol = expr[(*n)++]; switch (*symbol) { case '(': return lparen; case ')': return rparen; case '+': return plus; case '-': return minus; case '/': return divide; case '*': return times; case '%': return mod; case '\0': return eos; default: return operand; } }
- The expression is read one character at a time; each character is a token.
- Operands are assumed to be single digits. To turn a digit into a number:
symbol − '0'. The ASCII code of '2' is 50 and of '0' is 48: 50 − 48 = 2. - The expression ends with
'\0'→eos. exprcould have been a global array; here it is passed as a parameter.
Postfix Evaluation
Postfix Evaluation: eval (Global Version)
float eval(void) { char symbol; precedence token; float op1, op2; int n = 0, top = -1; token = get_token(expr, &symbol, &n); while (token != eos) { if (token == operand) push(&top, symbol - '0'); else { op2 = pop(&top); /* right operand first */ op1 = pop(&top); push(&top, apply(token, op1, op2)); } token = get_token(expr, &symbol, &n); } return pop(&top); /* the one left is the result */ }
- An operand is pushed; an operator pops two operands, applies itself, and pushes the result.
- The first one popped is op2 (the right operand):
6 2 −means 6 − 2, not 2 − 6. - At the end exactly one item must remain on the stack; otherwise the expression is malformed.
applypicks among the five operators with aswitch;case mod: (int)op1 % (int)op2, since%doesn't work on floats.
Postfix Evaluation
From Globals to Parameters
/* global version: external dependencies */ char expr[] = "42/2-33*+42*-"; float stack[MAX_SIZE]; float eval(void);
The expression and stack are global: you can't evaluate two expressions at once or reuse the routine in another program.
/* component: all parameters/locals */ float eval(const char *expr); /* the stack is a local array in eval */ /* in main: */ float a = eval("23*54*+9-"); float b = eval("42/2-33*+42*-");
The same routine works anywhere, on any expression, with no side effects. The code traces use this version.
Postfix Evaluation · Run the code step by step
Code Trace: eval("23*54*+9-")
Expression 23*54*+9-; the stack is empty.
'2' operand: push '2' − '0' = 2.
'3' operand: push '3' − '0' = 3.
'*' operator: op2 = 3, op1 = 2 → push 2 * 3 = 6.
'5' operand: push '5' − '0' = 5.
'4' operand: push '4' − '0' = 4.
'*' operator: op2 = 4, op1 = 5 → push 5 * 4 = 20.
'+' operator: op2 = 20, op1 = 6 → push 6 + 20 = 26.
'9' operand: push '9' − '0' = 9.
'-' operator: op2 = 9, op1 = 26 → push 26 - 9 = 17.
'\0': the loop ends and the one item on the stack, 17, is returned. Each token is handled once → O(n).
float eval(const char *expr) { float y[MAX_SIZE], op1, op2; int top = -1; for (int n = 0; expr[n] != '\0'; n++) { char c = expr[n]; if (isdigit(c)) y[++top] = c - '0'; else { op2 = y[top--]; op1 = y[top--]; switch (c) { case '+': y[++top] = op1 + op2; break; case '-': y[++top] = op1 - op2; break; case '*': y[++top] = op1 * op2; break; case '/': y[++top] = op1 / op2; break; } } } return y[top]; }
Postfix Evaluation · Run the code step by step
Code Trace: Another Expression
Expression 42/2-33*+42*-; the stack is empty.
'4' operand: push '4' − '0' = 4.
'2' operand: push '2' − '0' = 2.
'/' operator: op2 = 2, op1 = 4 → push 4 / 2 = 2.
'2' operand: push '2' − '0' = 2.
'-' operator: op2 = 2, op1 = 2 → push 2 - 2 = 0.
'3' operand: push '3' − '0' = 3.
'3' operand: push '3' − '0' = 3.
'*' operator: op2 = 3, op1 = 3 → push 3 * 3 = 9.
'+' operator: op2 = 9, op1 = 0 → push 0 + 9 = 9.
'4' operand: push '4' − '0' = 4.
'2' operand: push '2' − '0' = 2.
'*' operator: op2 = 2, op1 = 4 → push 4 * 2 = 8.
'-' operator: op2 = 8, op1 = 9 → push 9 - 8 = 1.
'\0': the loop ends and the one item on the stack, 1, is returned. Each token is handled once → O(n).
float eval(const char *expr) { float y[MAX_SIZE], op1, op2; int top = -1; for (int n = 0; expr[n] != '\0'; n++) { char c = expr[n]; if (isdigit(c)) y[++top] = c - '0'; else { op2 = y[top--]; op1 = y[top--]; switch (c) { case '+': y[++top] = op1 + op2; break; case '-': y[++top] = op1 - op2; break; case '*': y[++top] = op1 * op2; break; case '/': y[++top] = op1 / op2; break; } } } return y[top]; }
Infix → Postfix
Infix → Postfix: the Algorithm
- Operand: written straight to the output.
- Operator: while the operator on the stack has precedence greater than or equal to the incoming one, pop it to the output; then push the incoming operator (popping on ties = left associativity).
- ( : always pushed; once inside the stack it acts as the lowest precedence.
- ): pop to the output until ( is seen; discard the (.
- At the end: pop everything left on the stack to the output.
| Token | isp (in stack) | icp (incoming) |
|---|---|---|
( | 0 | 20 |
* / % | 13 | 13 |
+ − | 12 | 12 |
# (bottom marker) | −1 | — |
( has two precedences: highest when arriving (it goes in without popping anything) and lowest on the stack (any operator can sit on top of it). The # at the bottom of the stack stops the loop.Infix → Postfix · Run the code step by step
Code Trace: a*(b+c)-d → Postfix
Expression a*(b+c)-d. A # (isp = −1) sits at the bottom of the stack; it is not drawn.
a operand → output.
*: isp(#) = -1 < icp(*) = 13 → push *.
(: isp(*) = 13 < icp(() = 20 → push (.
b operand → output.
+: isp(() = 0 < icp(+) = 12 → push +.
c operand → output.
): pop until ( → + to output; ( discarded.
-: isp(*) = 13 ≥ icp(-) = 12 → * to output; push -.
d operand → output.
End of input: the remaining - goes to the output. Result: abc+*d-. Each token is pushed and popped at most once → O(n).
void postfix(const char *e, char *out) { char y[MAX] = {'#'}; int top = 0, k = 0; for (; *e; e++) { if (isalnum(*e)) out[k++] = *e; else if (*e == ')') { while (y[top] != '(') out[k++] = y[top--]; top--; /* drop '(' */ } else { while (isp(y[top]) >= icp(*e)) out[k++] = y[top--]; y[++top] = *e; } } while (top > 0) out[k++] = y[top--]; out[k] = '\0'; }
Infix → Postfix · Run the code step by step
Code Trace: the a*(b+c)*d Example
Expression a*(b+c)*d. A # (isp = −1) sits at the bottom of the stack; it is not drawn.
a operand → output.
*: isp(#) = -1 < icp(*) = 13 → push *.
(: isp(*) = 13 < icp(() = 20 → push (.
b operand → output.
+: isp(() = 0 < icp(+) = 12 → push +.
c operand → output.
): pop until ( → + to output; ( discarded.
*: isp(*) = 13 ≥ icp(*) = 13 → * to output; push *.
d operand → output.
End of input: the remaining * goes to the output. Result: abc+*d*. Each token is pushed and popped at most once → O(n).
void postfix(const char *e, char *out) { char y[MAX] = {'#'}; int top = 0, k = 0; for (; *e; e++) { if (isalnum(*e)) out[k++] = *e; else if (*e == ')') { while (y[top] != '(') out[k++] = y[top--]; top--; /* drop '(' */ } else { while (isp(y[top]) >= icp(*e)) out[k++] = y[top--]; y[++top] = *e; } } while (top > 0) out[k++] = y[top--]; out[k] = '\0'; }
Infix → Postfix
Trace Table: a+b*c → abc*+
| token | [0] | [1] | top | Output |
|---|---|---|---|---|
| a | −1 | a | ||
| + | + | 0 | a | |
| b | + | 0 | ab | |
| * | + | * | 1 | ab |
| c | + | * | 1 | abc |
| eos | −1 | abc*+ |
- When
*arrives,+is on top: isp(+) = 12 < icp(*) = 13 →*goes on top of it. - At the end
*comes out first, then+: the higher-precedence operator is applied first.
postfix writes operands to the output immediately and empties the stack at the end, so the last operand is printed too.Infix → Postfix
Infix → Prefix: Reverse, Postfix, Reverse
(a+b)*c-d/aOne way to get infix → prefix: reverse the expression.
a/d-c*)b+a(Reversed. Now swap ( and ).
a/d-c*(b+a)Parentheses fixed. Convert this to postfix, but don't pop on equal precedence (use >), because reversing the expression also reverses associativity.
ad/cba+*-The postfix result.
-*+abc/daReverse it again: prefix. Same as row 3 of the table.
A direct way: scan the expression right to left; operands are prepended to the output, and operators follow the same stack rule. Both ways are O(n).
Infix → Postfix · Run the code step by step
Code Trace: Prefix Evaluation (-+*23*549)
Prefix -+*23*549 (infix: 2*3+5*4−9). Scanned right to left.
'9' operand → push.
'4' operand → push.
'5' operand → push.
'*': the first popped is op1 = 5 (the left operand!), op2 = 4 → 5 * 4 = 20.
'3' operand → push.
'2' operand → push.
'*': the first popped is op1 = 2 (the left operand!), op2 = 3 → 2 * 3 = 6.
'+': the first popped is op1 = 6 (the left operand!), op2 = 20 → 6 + 20 = 26.
'-': the first popped is op1 = 26 (the left operand!), op2 = 9 → 26 - 9 = 17.
Result 17: the same expression as the postfix example (23*54*+9−). In postfix the first pop was the right operand; in prefix it is the left one.
float prefix_eval(const char *e) { float y[MAX], op1, op2; int top = -1; for (int i = strlen(e) - 1; i >= 0; i--) { if (isdigit(e[i])) y[++top] = e[i] - '0'; else { op1 = y[top--]; op2 = y[top--]; y[++top] = apply(e[i], op1, op2); } } return y[top]; }
Infix → Postfix
Exercise: the Other Conversions
✓: done in this chapter · ?: conversions left to design.
EXERCISE: Design the remaining conversion and evaluation algorithms for expression parsing.
- postfix → infix (done by hand in this chapter; write it in C)
- postfix → prefix and prefix → postfix
- infix → prefix (directly, scanning right to left)
- prefix → infix
- evaluating infix directly (two stacks: operands and operators)
Other Applications · Run the code step by step
Code Trace: Balanced Brackets {a*(b+c)-[d]}
Input {a*(b+c)-[d]}.
{ opens → push.
'a' is not a bracket; skip it.
'*' is not a bracket; skip it.
( opens → push.
'b' is not a bracket; skip it.
'+' is not a bracket; skip it.
'c' is not a bracket; skip it.
) closes and matches the ( on top → pop.
'-' is not a bracket; skip it.
[ opens → push.
'd' is not a bracket; skip it.
] closes and matches the [ on top → pop.
} closes and matches the { on top → pop.
End of input and the stack is empty → true: balanced.
bool balanced(const char *e) { char y[MAX]; int top = -1; for (; *e; e++) { if (*e=='(' || *e=='[' || *e=='{') y[++top] = *e; else if (*e==')' || *e==']' || *e=='}') { if (top < 0 || !matches(y[top], *e)) return false; /* extra/wrong */ top--; } } return top == -1; /* none left open? */ }
Other Applications
Bracket Matching: Three Kinds of Error
| Input | What happens? | Error |
|---|---|---|
[(a+b]*c) | ] arrives with ( on top | Mismatch |
(a+b)) | The stack is empty when the second ) arrives | Extra closer |
((a+b) | ( is left on the stack at the end | Unclosed opener |
- A counter is enough for a single kind of bracket; with several kinds you need to know “the most recently opened” → a stack.
- Compilers, HTML/XML validators, and the matching-bracket highlighting in editors all use this algorithm.
- Time O(n); memory O(n) in the worst case (all openers).
Other Applications
More Applications of Stacks and Queues
Two stacks: each action goes on the undo stack, each undone action on the redo stack; a browser's back/forward buttons work the same way.
Inorder traversal becomes a loop with an explicit stack of node addresses: O(n) time, O(h) stack.
The first larger element to the right of each item, in one pass: a decreasing stack, O(n).
A deque finds the maximum of every window in O(n).
Use a queue in the maze and the first path found is a shortest one.
Recursive backtracking, or backtracking with an explicit stack.
Lab · C code step by step
Lab: Postfix Evaluation
Lab · C code step by step
Lab: Infix → Postfix
Summary
Complexity Summary
| Algorithm | Structure | Time | Extra memory |
|---|---|---|---|
| Maze (path) | stack | O(m·p) | O(m·p) |
| Maze shortest path | queue | O(m·p) | O(m·p) |
| Postfix / prefix evaluation | stack | O(n) | O(n) |
| Infix → postfix / prefix | stack | O(n) | O(n) |
| Bracket matching | stack | O(n) | O(n) |
| Non-recursive inorder | stack | O(n) | O(h) |
| Level-order traversal | queue | O(n) | O(n) |
n: expression length or number of nodes · m×p: maze size · h: tree height.
Summary
Common Mistakes
op1 and op2 in postfix: 6 2 − becomes −4.a−b−c converts wrongly.) arrives, leaving the ( on the stack or writing it to the output.mark in the maze and looping forever.Summary
Exercises
- Convert
a/(b-c)+d*(e-a)*cto postfix and prefix. - Evaluate the postfix expression
6 2 3 + − 3 8 2 / + *. - Write a C routine that converts postfix to infix.
- Make
pathmark the path it finds in the maze. - Find the shortest path through the maze with a queue.
- Write non-recursive preorder and postorder traversals.
Hints / answers
- Postfix:
abc-/dea-*c*+; prefix:+/a-bc**d-eac. - 2+3=5, 6−5=1, 8/2=4, 3+4=7, 1*7 = 7.
- Operand: push a one-character string; operator: b = pop, a = pop, push "(" a op b ")". Watch your malloc/free.
- Once found, set maze[r][c] = 2 for every (row, col) pair on the stack.
- Enqueue (row, col); for each cell remember the cell it came from; on reaching the exit, trace back.
- Preorder: push the root; loop: pop, print, push the right child and then the left. Postorder: two stacks or a visited flag.
Summary
Chapter Summary
Every fork in the maze goes on the stack; at a dead end, pop back to the last fork.
A long, wrong first path: the whole wrong region gets explored.
Infix needs precedence; postfix/prefix are evaluated without parentheses or precedence.
Push operands; on an operator pop op2, then op1; push the result. O(n).
Pop while isp ≥ icp; ( is high when arriving, low on the stack.
Wherever you see “last opened, first closed,” use a stack; wherever you see “in order,” use a queue.
Summary
References and Next Steps
- E. Horowitz, S. Sahni, S. Anderson-Freed, Fundamentals of Data Structures in C, 2nd ed., Sections 3.5 (mazes) and 3.6 (evaluation of expressions).
- R. Sedgewick, Algorithms in C, Parts 1–4, Sections 4.3 (postfix) and Chapter 5 (recursion and stacks).