A library with a linked list
The problem
Build a program that keeps a library's books in a linked list, and lets you add, remove, borrow and return them.
Requirements
- Each book is a node on the heap, holding a title and whether it is on loan
- New books are inserted so that the list stays in alphabetical order
- Removing a book works for the first, a middle and the last node
- Borrowing an unknown or a loaned book gives a clear message
- A count of the books on loan is shown on request
- Every node is freed when the program ends
Technologies
- C
- Pointers
- Structs
- malloc and free
- Strings
Step by step
- Define the node struct, and write a function that creates a node.
- Write the list printing function.
- Write sorted insertion, handling the empty list and a new first node.
- Write the find function, and use it for borrowing and returning.
- Write removal, handling each position in the list.
- Write a function that frees the entire list.
- Test each operation on an empty list and on a one-book list.
Expected result
A working library program that correctly maintains a sorted linked list, with no leaks.
How it is evaluated
- The list stays sorted after every operation
- Removal is correct in every position
- No memory is leaked
- Strings are copied with a size limit
Ways to improve it
- Save the library to a file
- Keep a borrower's name for each loan
- Use a doubly linked list