DSA Notes
Build a library management system that demonstrates practical use of hash tables for book lookup, BST for sorted catalogs, queues for waitlists, stacks for undo operations, and linked lists for borrow history.
Project Overview
This project shows how different data structures solve different problems within a single application. A library management system needs to:
- Look up books by ISBN instantly → Hash Table
- Browse books in sorted order (by title/author) → Binary Search Tree
- Manage waitlists for popular books → Queue
- Support undo/redo for librarian actions → Stack
- Track borrow history for each member → Linked List
Let's build each component and see why that specific data structure is the right choice.
Component 1: Hash Table for Book Lookup
When someone walks up with an ISBN, you need O(1) lookup. A hash table maps ISBN → book details:
Why hash table? Libraries have thousands of books. Searching by ISBN must be instant — O(1) average. No other data structure gives us this for key-value lookup.
Component 2: BST for Sorted Catalog Browsing
Users want to browse books alphabetically or find books in a title range. A BST gives us sorted order naturally:
Why BST? We need sorted order AND dynamic insertions. A sorted array gives browsing but O(n) inserts. A BST gives O(log n) for both (when balanced).
Component 3: Queue for Book Waitlists
When all copies are checked out, members join a waitlist. First-come, first-served — this is exactly FIFO (queue):
Why queue? Fairness demands first-come-first-served. A queue guarantees O(1) enqueue and dequeue while maintaining insertion order.
Component 4: Stack for Undo/Redo
Librarians make mistakes — check out to wrong member, add wrong book, etc. An undo system uses a stack:
Why stack? Undo is always "reverse the most recent action" — that is LIFO. The stack naturally gives us the most recent action on top.
Component 5: Linked List for Borrow History
Each member has a chronological history of borrowed books. We frequently add to the front (newest first) and display recent history:
class HistoryNode:
def __init__(self, isbn, title, borrow_date, return_date=None):
self.isbn = isbn
self.title = title
self.borrow_date = borrow_date
self.return_date = return_date
self.next = None
class BorrowHistory:
def __init__(self, member_id):
self.member_id = member_id
self.head = None # Most recent borrow
self.count = 0
def add_borrow(self, isbn, title, date):
"""Add new borrow record at the front (most recent first)."""
node = HistoryNode(isbn, title, date)
node.next = self.head
self.head = node
self.count += 1
def mark_returned(self, isbn, return_date):
"""Mark a book as returned."""
current = self.head
while current:
if current.isbn == isbn and current.return_date is None:
current.return_date = return_date
return True
current = current.next
return False
def get_recent(self, n=10):
"""Get the n most recent borrow records."""
records = []
current = self.head
while current and len(records) < n:
records.append({
'isbn': current.isbn,
'title': current.title,
'borrowed': current.borrow_date,
'returned': current.return_date
})
current = current.next
return records
def currently_borrowed(self):
"""List all books not yet returned."""
borrowed = []
current = self.head
while current:
if current.return_date is None:
borrowed.append((current.isbn, current.title))
current = current.next
return borrowedWhy linked list? History is append-heavy and traversal-based. We never need random access to "the 47th book borrowed." We always traverse from recent to old. Linked list gives O(1) prepend and natural sequential traversal.
Putting It All Together
Lessons Learned
This project demonstrates the right tool for the right job principle:
- Hash tables for key-based lookup
- BSTs for sorted access patterns
- Queues for fairness/ordering
- Stacks for reversal/undo
- Linked lists for sequential history
No single data structure does everything well. Good software engineering means choosing the right structure for each access pattern.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Library Management System Using DSA.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, projects, library, management
Related Data Structures & Algorithms Topics