Capstone: Linked List in C
A singly linked list chains heap nodes (value plus next pointer) through head-pointer surgery: prepend links a fresh node atop, append walks to the tail, delete unlinks and frees, destroy walks the whole chain. Every operation is pointer manipulation learned across the pointers, structs, and memory modules — the course's comprehensive final exam.
Lists trade cache-hostile O(n) indexing for O(1) head insertion and never-invalidated node addresses. Implement prepend/append/find/delete/destroy plus a print helper, test under AddressSanitizer until clean, and the language's hardest mechanics become muscle memory.
Nodes chain via next
struct Node { int value; struct Node *next; }; — each node points onward; NULL terminates the chain.
Prepend in O(1)
New node's next takes the head, head takes the node — constant-time front insertion, the list's superpower.
Unlink then free
Bypass the victim (prev->next = victim->next, or move head), then free it — order matters, reversed order dangles.
Destroy by walking
Save next, free current, advance — the only safe traversal order, verified leak-free under sanitizers.
#include <stdio.h>#include <stdlib.h> typedef struct Node { int value; struct Node *next;} Node; void prepend(Node **head, int v) { Node *n = malloc(sizeof *n); if (!n) return; n->value = v; n->next = *head; *head = n;} int main(void) { Node *head = NULL; prepend(&head, 2); prepend(&head, 1); printf("%d %d\n", head->value, head->next->value); return 0;}Per-node malloc overhead; node addresses never invalidate.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.