Lesson 5 of 5
Mini Project Part 7 - Struct-Based Linked List
Build a singly-linked list using heap-allocated nodes with a self-referential struct. Implements: push_front, push_back, remove_by_value, print, reverse, and free_all with correct memory management. This project ties together structs, pointers, malloc/free, and the ownership model.
Linked List - Complete Implementation with Memory Safety
/* list.c - singly linked list with proper memory management */
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
typedef struct {
Node *head;
size_t size;
} List;
/* Create an empty list */
void list_init(List *l) { l->head = NULL; l->size = 0; }
/* Allocate a single node */
static Node *make_node(int val) {
Node *n = malloc(sizeof(Node));
if (!n) { fprintf(stderr, "malloc failed
"); return NULL; }
n->data = val;
n->next = NULL;
return n;
}
/* Push to front - O(1) */
int list_push_front(List *l, int val) {
Node *n = make_node(val);
if (!n) return -1;
n->next = l->head;
l->head = n;
l->size++;
return 0;
}
/* Push to back - O(n) */
int list_push_back(List *l, int val) {
Node *n = make_node(val);
if (!n) return -1;
if (l->head == NULL) { l->head = n; l->size++; return 0; }
Node *cur = l->head;
while (cur->next) cur = cur->next;
cur->next = n;
l->size++;
return 0;
}
/* Remove first node with matching value - O(n) */
int list_remove(List *l, int val) {
Node **prev_next = &l->head; /* pointer to the *next pointer to update */
while (*prev_next) {
Node *cur = *prev_next;
if (cur->data == val) {
*prev_next = cur->next; /* unlink */
free(cur);
l->size--;
return 1; /* removed */
}
prev_next = &cur->next;
}
return 0; /* not found */
}
/* Print list */
void list_print(const List *l) {
printf("List[%zu]: ", l->size);
for (Node *cur = l->head; cur; cur = cur->next)
printf("%d -> ", cur->data);
printf("NULL
");
}
/* Reverse in-place - O(n), O(1) space */
void list_reverse(List *l) {
Node *prev = NULL, *cur = l->head, *next = NULL;
while (cur) {
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
l->head = prev;
}
/* Free all nodes */
void list_free(List *l) {
Node *cur = l->head;
while (cur) {
Node *tmp = cur->next;
free(cur);
cur = tmp;
}
l->head = NULL;
l->size = 0;
}
int main(void) {
List l;
list_init(&l);
list_push_back(&l, 1);
list_push_back(&l, 2);
list_push_back(&l, 3);
list_push_front(&l, 0);
list_print(&l); /* 0->1->2->3->NULL */
list_remove(&l, 2);
list_print(&l); /* 0->1->3->NULL */
list_reverse(&l);
list_print(&l); /* 3->1->0->NULL */
list_free(&l);
list_print(&l); /* List[0]: NULL */
return 0;
}
/* Build: gcc -Wall -Wextra -std=c99 list.c -o list
Run with valgrind: valgrind --leak-check=full ./list */Common Mistakes
- Not saving `cur->next` before freeing in list_free - `free(cur); cur = cur->next;` - you read `cur->next` AFTER freeing `cur`. Save it first: `Node *tmp = cur->next; free(cur); cur = tmp;`.
- Forgetting to handle the head pointer update in remove - when removing the head node, `l->head` must be updated to `head->next`. The `Node **prev_next = &l->head` pattern elegantly handles this case without a special case.
- Not setting `l->head = NULL` after list_free - leaves a dangling head pointer. Any subsequent access to the list would be undefined behaviour.
Tip
Tip
Practice Mini Project Part 7 StructBased Linked List in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Linked list nodes connect dynamically via pointers, supporting efficient O(1) head insertions
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Part 7 StructBased Linked List from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Mini Project Part 7 StructBased Linked List is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- Build a singly-linked list using heap-allocated nodes with a self-referential struct.
- Not saving `cur->next` before freeing in list_free - `free(cur); cur = cur->next;` - you read `cur->next` AFTER freeing `cur`. Save it first: `Node *tmp = cur->next; free(cur); cur = tmp;`.
- Forgetting to handle the head pointer update in remove - when removing the head node, `l->head` must be updated to `head->next`. The `Node **prev_next = &l->head` pattern elegantly handles this case without a special case.
- Not setting `l->head = NULL` after list_free - leaves a dangling head pointer. Any subsequent access to the list would be undefined behaviour.
Finished this lesson?