By the end of this lesson, cadets will be able to:
prev/curr two-pointer splice. (Outcome 2)free_list as the per-node delete pattern applied to every node in a loop. (Outcome 1)Complete before class. The supplementary notes are the primary reading; they carry the deletion mechanics this lesson is built on.
| Source | What | Why |
|---|---|---|
| Lesson 30 Supplementary Notes (instructor-written) | Deletion: the two-pointer splice, edge cases, freeing in order | The core mechanics you will use in the walkthrough and the lab |
| Your Lesson 29 notes and code | Review node structure, traversal, and free_list |
You build directly on last lesson's playlist |
prev/curr two-pointer walkprev->next = curr->next;playlist.c. Confirm it compiles cleanly before class so you can spend class time on deletion, not on fixing last lesson's code.Deletion is where the pointer discipline from last lesson earns its keep. Inserting only ever added links; deletion removes them, and a removed node also owns memory that has to be returned. Get the order of operations wrong and you either leak memory or read memory you have already freed. The reference material below walks each piece you will see in lecture and practice in the walkthrough.
The Lesson 30 deck covers the deletion problem, the prev/curr walk, the splice, freeing in the right order, the four edge cases, and how free_list is just deletion in a loop. The slides anchor the lecture; the walkthrough is where you practice each move hands-on.
To remove a node from the middle of a chain, you have to make the node before it point to the node after it, so nothing in the list still routes through the one you are removing. In a singly-linked list each node only points forward, so a node has no way to find the one ahead of it. That single fact drives everything about deletion: you must arrive at the victim already holding a pointer to its predecessor.
The fix is to walk the list with two pointers instead of one. curr finds the node you want; prev trails exactly one step behind it. When curr lands on the target, prev is already sitting on the predecessor, which is the node whose next you need to rewire.
With prev on the predecessor and curr on the victim, one assignment removes the node from the chain:
This one line handles the tail case for free: if curr is the last node, curr->next is already NULL, so prev->next becomes NULL and the list ends correctly.
A node owns two heap allocations: the node struct, and the title string it points to. After you unlink the node you must free both. Order matters:
If you free the node first, curr->title is unreachable: the string leaks and the read itself is a use-after-free. free never follows pointers inside a struct for you, so it will not free the title on its own.
| Case | What is different |
|---|---|
| Empty list | Nothing to delete. Return NULL immediately; never dereference a NULL head. |
| Deleting the head | The head has no predecessor. Move head forward first, then free the old node, and return the new head. |
| Single node | Deleting the only node empties the list. The list collapses to NULL. |
| Deleting the tail | No node names the tail, so walk prev/curr until curr->next == NULL. The general splice handles it. |
In Lesson 29 you were handed free_list and told to take it on faith. Now you can read it for what it is: the per-node delete you just learned (save the next pointer, free the title, free the node) applied to every node in a loop. It walks the chain once, freeing both allocations of each node in order, until it runs off the end at NULL. free_list stays provided in the lab; you do not rewrite it. Your job is the three single-node delete functions.
Three things to do in class. Use the checklist to track your progress.
delete_head, delete_tail, and delete_by_title to your Lesson 29 code. Auto-graded through GitHub Classroom.
Fourteen tasks across three sections. The playlist starts with three songs already built. Type each C statement at the prompt; the terminal narrates what happened and the diagram shows the chain, the prev/curr walkers, and any node you have freed.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Lesson 30 reflection.
Six questions. Answer each one and submit to see how you did. The final summary gives you a completion code and tells you whether you are ready for the lab or should replay the walkthrough first.
Enter your name. It will appear on the score card so you can include it in your screenshot for the Lesson 30 reflection.