Skip to content
C project #18Intermediate

To-Do Manager (Linked List)

A command-driven to-do list built on a singly linked list kept in priority order, with completion, removal, reversal and persistence.

Estimated time
~5 hours to build
Files
7 files · 425 lines
Technologies
C11, Linked lists

Introduction

Linked lists are the first dynamic data structure most students build. This project uses one for a practical purpose and shows the elegant pointer-to-pointer technique that removes special cases when inserting and deleting.

Problem statement

Implement a to-do manager with commands: add a task with a priority (1–5), mark a task done, remove a task, clear completed tasks, reverse the list and list tasks. Tasks are kept in priority order and saved to a file.

Objectives

  • Implement insertion, deletion and reversal of a singly linked list
  • Use pointer-to-pointer (Task **) to avoid head special cases
  • Free every node
  • Parse simple text commands

Features

  • Priority-ordered insertion (stable for equal priorities)
  • Done / remove / clear completed
  • In-place reversal
  • Text persistence

Concepts demonstrated

  • Singly linked lists
  • Pointer to pointer
  • Dynamic memory
  • Command parsing

System architecture

Each module pairs a header (.h, the interface) with a source file (.c, the implementation); main.c contains only the user interface. insert_sorted walks a Task **link until it finds where the new task belongs and rewires one pointer — the same code handles inserting at the head, middle or end. todo_remove and todo_clear_done use the same trick. todo_reverse re-points each node's next to its predecessor.

TextArchitecture diagram
head ──> [P1 #2] ──> [P1 #4] ──> [P2 #1] ──> [P3 #3] ──> NULL
▲ insert P1 here (after equal priorities)

Folder structure

TextProject tree
todo-linked-list/
├── README.md
├── Makefile
├── src/main.c
├── src/todo.c
├── src/todo.h
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -O2
SRC := $(wildcard src/*.c)
HDR := $(wildcard src/*.h)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CC) $(CFLAGS) -Isrc $(SRC) -o app -lm
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/todo.h
The Task node and TodoList types and the API.
src/todo.c
Linked-list operations, printing and file persistence.
src/main.c
The command loop and argument parsing.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
gcc -std=c11 -Wall -Wextra -Isrc src/*.c -o app -lm
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
Commands: add <priority 1-5> <text> | done <id> | rm <id> | clear | rev | list | quit
>
Added task #1.
Commands: add <priority 1-5> <text> | done <id> | rm <id> | clear | rev | list | quit
>
Added task #2.
Commands: add <priority 1-5> <text> | done <id> | rm <id> | clear | rev | list | quit
>
Added task #3.
Commands: add <priority 1-5> <text> | done <id> | rm <id> | clear | rev | list | quit
>
Added task #4.
Commands: add <priority 1-5> <text> | done <id> | rm <id> | clear | rev | list | quit
>
[ ] #2 P1 Submit project report
[ ] #4 P1 Pay hostel fees
[ ] #1 P2 Finish DBMS assignment
[ ] #3 P3 Read chapter on pointers
4 task(s), 4 pending
Commands: add <priority 1-5> <text> | done <id> | rm <id> | clear | rev | list | quit
>
Task #2 completed.
Commands: add <priority 1-5> <text> | done <id> | rm <id> | clear | rev | list | quit
>
Task #4 completed.
Commands: add <priority 1-5> <text> | done <id> | rm <id> | clear | rev | list | quit
>
[x] #2 P1 Submit project report
[x] #4 P1 Pay hostel fees
[ ] #1 P2 Finish DBMS assignment
[ ] #3 P3 Read chapter on pointers
4 task(s), 2 pending
Commands: add <priority 1-5> <text> | done <id> | rm <id> | clear | rev | list | quit
>
Removed 2 completed task(s).
Commands: add <priority 1-5> <text> | done <id> | rm <id> | clear | rev | list | quit
>
List reversed (until the next add, which re-sorts by priority).
[ ] #3 P3 Read chapter on pointers
[ ] #1 P2 Finish DBMS assignment
2 task(s), 2 pending
Commands: add <priority 1-5> <text> | done <id> | rm <id> | clear | rev | list | quit
>
No task #9.
Commands: add <priority 1-5> <text> | done <id> | rm <id> | clear | rev | list | quit
>
Usage: add <1-5> <text>
Commands: add <priority 1-5> <text> | done <id> | rm <id> | clear | rev | list | quit
>
Saved 2 task(s).
Input
add 2 Finish DBMS assignment
add 1 Submit project report
add 3 Read chapter on pointers
add 1 Pay hostel fees
list
done 2
done 4
list
clear
rev
rm 9
add 6 invalid priority
quit

Testing

  • Diff with tests/expected-output.txt.
  • Run under AddressSanitizer (-fsanitize=address) and confirm there are no leaks.
  • Add tasks with equal priority and check they stay in insertion order.

Common errors

Segfault when deleting the first task

Using Task **link avoids the special case; if you use a plain prev pointer you must update head separately.

Memory leak reported

todo_free must walk the list and free every node, saving next before freeing.

Possible improvements

  • Due dates and sorting by date
  • Undo the last command
  • Doubly linked list for backward traversal

Additional challenges

  1. 1Implement merge sort on the linked list.
  2. 2Detect a cycle with Floyd's algorithm (create one deliberately to test).
  3. 3Add an edit <id> <text> command.

Built and tested it yourself? Mark it to update your dashboard.