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
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.
Folder structure
Complete source code
Makefile
What each file does
- src/todo.h
- The
Tasknode andTodoListtypes 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:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
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
- 1Implement merge sort on the linked list.
- 2Detect a cycle with Floyd's algorithm (create one deliberately to test).
- 3Add an
edit <id> <text>command.
Built and tested it yourself? Mark it to update your dashboard.