Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Blackboard reflection.
Enter your name. It will appear on the score card so you can include it in your screenshot for the Blackboard reflection.
By the end of this lesson, cadets will be able to:
Complete before class. There is no Beej chapter for this lesson; the supplementary notes are your reading.
| Source | Sections | Why |
|---|---|---|
| Lesson 31 supplementary notes (instructor-written) | All seven sections | Stacks and queues on linked lists, the back pointer, the empty-queue reset |
They walk through the stack, the queue, the back pointer, and the two empty-queue special cases. They are short and they are the reading; do not skip them.
You push 3, then 5, then 8 onto a stack, then pop once. What comes back? Now enqueue the same three values onto a queue and dequeue once. What comes back?
The stack returns 8 (last in, first out). The queue returns 3 (first in, first out). Same three inputs, opposite ends. That contrast is the whole lesson.
For two lessons you have built and modified a singly linked list. Today the node does not change at all; what changes is the rule about where you add and remove. That single rule turns a plain list into a stack or a queue, the two most common ADTs in everyday programming. The reference below covers what you will see in lecture and practice in the two widgets.
The Lesson 31 deck covers the stack, the queue, why the queue needs a back pointer, the two empty-queue special cases, and how to choose between the two ADTs. The slides are the spine of the lecture; the two widgets are where you practice.
A stack tracks a single pointer, top, aimed at the most recently added node. push makes a new node, points its next at the old top, and becomes the new top. pop removes the top, moves the pointer down, and returns the value. peek reads the top without removing it. Every operation touches only the top, so all three are O(1) with no walk down the chain. This is the insert-at-head from Lesson 29, renamed.
void history_push(history_t *h, const char *url) { page_t *node = malloc(sizeof(page_t)); node->url = copy_string(url); /* your own heap copy */ node->next = h->top; /* point at the old top */ h->top = node; /* become the new top */ h->count++; }
A queue adds at the back and removes from the front. enqueue links a new node after the current back and advances the back to it. dequeue removes the front node and returns its value. peek reads the front. The split, adding at one end and removing at the other, is the entire difference from a stack. Same node, opposite access pattern.
Two moments need care. Enqueuing into an empty queue must set both front and back to the new node, since there is no old back to link after. And dequeuing the last node must reset back to NULL along with front; see the next subsection.
Removing from the front is easy: front is right there. Adding at the back is the problem. With only a front pointer, every enqueue would walk the whole chain to find the last node, which is O(n). A second pointer that always aims at the last node makes enqueue a single step, O(1). That is the whole reason a queue carries back.
The trap that follows: when the last node leaves and the queue empties, front becomes NULL but back is left pointing at the node you just freed. The queue looks empty by front, but the next enqueue writes through that dangling back and corrupts memory. When the queue empties, reset both ends.
char *dequeue(queue_t *q) { if (queue_is_empty(q)) return NULL; ticket_t *node = q->front; char *summary = node->summary; /* hand value to caller */ q->front = node->next; if (q->front == NULL) q->back = NULL; /* reset back too! */ q->count--; free(node); /* free node, not the string */ return summary; }
Ask one question: in what order should things come back out? Most recent first, or "undo the last thing," means a stack. Oldest first, or "serve in arrival order," means a queue. The order the problem demands picks the structure. Both are O(1) on a linked list, so you are choosing on meaning, not speed.
Today's two labs make the contrast concrete: a browser Back button is a stack (most recent page first), and a help-desk ticket line is a queue (oldest ticket served first).
Lesson 31 introduces no new DFCS C Programming Standard section. The memory-cleanup rule (§8.5, free what you allocate) still applies and is checked in both labs with valgrind, but it was introduced earlier and is enforced here, not retaught. The full standard lives in the course Resources section.
Five things to do in class. Use the checklist to track your progress.
Seventeen tasks across three sections. Type each operation at the prompt. The terminal shows what the operation did; the structure pane draws the node chain, the pointers tracking its ends, and the value each removal hands back.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Blackboard reflection.
Six questions covering which ADT a problem needs, tracing LIFO and FIFO removal, why a queue keeps a back pointer, and what to reset when a queue empties. Pass at 70% or above; below that, replay the walkthrough and take the quiz again.
Enter your name. It will appear on the score card so you can include it in your screenshot for the Blackboard reflection.