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
- 10 index cards per pair of students
- Whiteboard or projection of the operation trace tables below
Vocabulary
- Data Structure: A deliberate way of organizing data in memory so specific operations (inserting, removing, searching) are fast and predictable.
- Stack (LIFO — Last-In, First-Out): A linear collection where items are added (
PUSH), removed (POP), and inspected without removal (PEEK) at the same end (the top).
- Queue (FIFO — First-In, First-Out): A linear collection where items are added (
ENQUEUE) at the back (tail) and removed (DEQUEUE) from the front (head).
- Constant Time (
O(1)): An operation whose step count stays the same whether the collection holds 5 items or 5,000,000 items.
Lesson flow
1. Opening: Two Ways to Handle a Pile of Tasks (8 min)
- Contrast two everyday situations:
- 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!)
- 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!)
- 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)
- Explain how code editors and compilers check whether parentheses, square brackets, and curly braces—
(), [], {}—are properly nested:
- Algorithm: Scan the string left to right:
- If you see an opening bracket (
(, [, or {), PUSH it onto the stack.
- 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).
- At the end of the string, if the stack is empty, return
VALID; if any brackets remain on the stack, fail (Unclosed opening bracket).
- Student pairs use their index cards as a physical stack to trace three expressions and record the exact step where invalid strings fail:
- Expression 1:
{ [ ( ) ] ( ) } → Stack ends empty → VALID.
- Expression 2:
[ ( ] ) → Push [, Push (, next character is ], pop ( → mismatch (( does not match ]) at character 3 → INVALID.
- Expression 3:
( [ { } ] → Ends with ( still on the stack → INVALID (unclosed ().
4. Discussion & Operation Cost Check (5 min)
- Question: "If an array holds
1,000,000 items in slots 0 through 999,999, why is PUSH / POP at the end of the array (slot 999,999) a 1-step O(1) operation, whereas removing from slot 0 and sliding all 999,999 remaining items one slot left takes 999,999 steps (O(n))?"
- Example good answer: Adding or removing at the end only touches one slot (
top), whereas shifting every item left touches every remaining element in the array. (To make a Queue O(1) for both ENQUEUE and DEQUEUE, languages use a circular buffer with a moving front index or a doubly linked list like Python's collections.deque.)
5. Closing check (5 min)
- 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?
- 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?
- 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
- 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).
- 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).
- 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
- Extra support: Have students write the index number (
1, 2, 3...) on each card as it enters and physically stack cards vertically for a Stack vs. slide cards horizontally through a paper tunnel for a Queue.
- Extra challenge: Show how a browser's Back and Forward buttons use two stacks (
back_stack and forward_stack): clicking Back pops the current page from back_stack and pushes it onto forward_stack, while visiting a brand-new link clears forward_stack.
Teacher notes
- Common misconception: Students sometimes think a stack "sorts" items by size. Emphasize that stacks and queues care only about arrival time, never about whether
10 is smaller or larger than 40.