Lesson 1: Who Goes Next? — Stacks (LIFO), Queues (FIFO), and Operation Cost

Learning objective

Students will trace PUSH / POP / PEEK operations on a stack (LIFO) and ENQUEUE / DEQUEUE operations on a queue (FIFO), count the exact steps required for each operation, and use a stack to verify whether a string of nested brackets is balanced.

Materials

Vocabulary

Lesson flow

1. Opening: Two Ways to Handle a Pile of Tasks (8 min)

  1. Contrast two everyday situations:
  2. Situation A (Cafeteria Tray Dispenser / Text Editor Undo): You type "cat", then "dog", then "bird". When you press Ctrl+Z (Undo), which word should disappear first? ("bird"—the last action entered must be the first action undone!)
  3. Situation B (Cafeteria Lunch Line / Network Router Packet Buffer): Three packets P1, P2, P3 arrive at a router (Grade 6 Computing) in that order. Which packet should leave the router first? (P1—the first packet in line must be the first packet sent!)
  4. Introduce the names: Situation A is a Stack (LIFO); Situation B is a Queue (FIFO).

2. Direct instruction: Tracing Stack vs. Queue State Tables (15 min)

Walk through the exact same arrival sequence (A, then B, then C, followed by two removals, then D, then one removal) on both structures:

Step Stack Command Stack State (bottom → top) Returned Value Queue Command Queue State (front → back) Returned Value
1 PUSH(A) [A] — ENQUEUE(A) [A] —
2 PUSH(B) [A, B] — ENQUEUE(B) [A, B] —
3 PUSH(C) [A, B, C] — ENQUEUE(C) [A, B, C] —
4 POP() [A, B] C DEQUEUE() [B, C] A
5 POP() [A] B DEQUEUE() [C] B
6 PUSH(D) [A, D] — ENQUEUE(D) [C, D] —
7 POP() [A] D DEQUEUE() [D] C

Highlight the output order: - The Stack popped C, B, D (always removing the newest item on top). - The Queue dequeued A, B, C (always removing the oldest item waiting at the front).

3. Main activity: Bracket-Matching Compiler Check with a Stack (17 min)

  1. Explain how code editors and compilers check whether parentheses, square brackets, and curly braces—(), [], {}—are properly nested:
  2. Algorithm: Scan the string left to right:
    1. If you see an opening bracket ((, [, or {), PUSH it onto the stack.
    2. If you see a closing bracket (), ], or }):
      • If the stack is empty, fail immediately (Unmatched closing bracket).
      • Otherwise, POP the top opening bracket and check if it matches the closing bracket's type. If not, fail (Mismatched bracket type).
    3. At the end of the string, if the stack is empty, return VALID; if any brackets remain on the stack, fail (Unclosed opening bracket).
  3. Student pairs use their index cards as a physical stack to trace three expressions and record the exact step where invalid strings fail:
  4. Expression 1: { [ ( ) ] ( ) } → Stack ends empty → VALID.
  5. Expression 2: [ ( ] ) → Push [, Push (, next character is ], pop ( → mismatch (( does not match ]) at character 3 → INVALID.
  6. Expression 3: ( [ { } ] → Ends with ( still on the stack → INVALID (unclosed ().

4. Discussion & Operation Cost Check (5 min)

5. Closing check (5 min)

  1. Start with an empty stack S. Execute: PUSH(10), PUSH(20), POP(), PUSH(30), PUSH(40), POP(). What value is returned by the second POP(), and what items remain in S from bottom to top?
  2. Start with an empty queue Q. Execute: ENQUEUE(10), ENQUEUE(20), DEQUEUE(), ENQUEUE(30), ENQUEUE(40), DEQUEUE(). What value is returned by the second DEQUEUE(), and what items remain in Q from front to back?
  3. Trace the bracket-matching algorithm on ( [ ) ]. What is on the stack right before the first closing bracket ) is read, and why does the check fail?

Worked answers for closing check

  1. First POP() removes 20 (leaving [10]). Then 30 and 40 are pushed ([10, 30, 40]). The second POP() returns 40, leaving [10, 30] (10 at bottom, 30 at top).
  2. First DEQUEUE() removes 10 (leaving [20]). Then 30 and 40 are enqueued ([20, 30, 40]). The second DEQUEUE() returns 20, leaving [30, 40] (30 at front, 40 at back).
  3. Right before ) is read, the stack holds [ '(', '[' ] (with '[' on top). When ) is read, POP() returns '[', which does not match ')', so the algorithm immediately reports a mismatch failure.

Differentiation

Teacher notes