Two approaches to efficient book cataloging and retrieval
A comprehensive library management system with custom hash table and STL map implementations. Demonstrates collision handling, CRUD operations, and persistent file storage.
|
|
Hash Table (size = 100)
┌─────┬─────┬─────┬─────┬─────┐
│ 0 │ 1 │ 2 │ ... │ 99 │ (Buckets)
└──┬──┴─────┴──┬──┴─────┴─────┘
│ │
▼ ▼
[101] [202] → [302] → NULL (Chaining via Linked Lists)
│ │ │
Book A Book B Book C
Hash Function: h(key) = (key × 31) % 100
| 🔗 Custom Hash Table | 🗺️ STL Unordered Map |
|---|---|
|
File: struct HashNode {
int val;
Book books;
HashNode* next;
};
vector<HashNode*> table;Pros:
Cons:
|
File: unordered_map<int, Book> library;Pros:
Cons:
|
| Operation | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Insert | O(1) average, O(n) worst | O(1) | Worst case when all keys collide |
| Search | O(1) average, O(n) worst | O(1) | Linear search in collision chain |
| Update | O(1) average, O(n) worst | O(1) | Search + modify |
| Delete | O(1) average, O(n) worst | O(1) | Search + unlink node |
| Load from File | O(n) | O(n) | n = number of books |
| Save to File | O(n + m) | O(1) | n = books, m = table size |
Load Factor: Books / Table Size (optimal: 0.7-0.8)
# Custom Hash Table Implementation
g++ -o lms_hash LMSUsingLinkedList.cpp -std=c++11
# STL Map Implementation
g++ -o lms_map LMSUSingMap.cpp -std=c++11$ ./lms_hash
Enter The File Name You Want To Read/Write To (e.g., library.txt): library.txt
Library data loaded successfully!
--- Library Menu (Custom Hash Table) ---
1. Add A Book
2. Find A Book
3. Update A Book
4. Remove A Book
Enter The Choice: 2
Enter The Id: 101
--- Book Found ---
Title: To Kill a Mockingbird
Author: Harper Lee
Checkout Status: No| Action | Command Flow |
|---|---|
| Add Book | Choice 1 → Enter ID → Enter Title → Enter Author |
| Search | Choice 2 → Enter ID → View Details |
| Update | Choice 3 → Enter ID → Select Field → Enter New Value |
| Delete | Choice 4 → Enter ID → Confirm Removal |
| Exit | Any other number → Auto-saves to file |
library.txt (50 books included)
101 ← Book ID (integer)
To Kill a Mockingbird ← Title (string, can have spaces)
Harper Lee ← Author (string, can have spaces)
0 ← Checkout Status (0 = available, 1 = checked out)
102
1984
George Orwell
1
...
Format Rules:
- Each book occupies exactly 4 lines
- ID must be unique (enforced by program)
- Status:
0= Available,1= Checked Out - File is auto-generated on exit
Sample Dataset: Includes 50 classic books (IDs 101-150)
LibraryManagementSystem/
├── 📄 LMSUsingLinkedList.cpp # Custom hash table (350 lines)
├── 📄 LMSUSingMap.cpp # STL unordered_map implementation
├── 📊 library.txt # Persistent storage (50 books)
└── 📖 Readme.md # This file
| Component | Responsibility |
|---|---|
| Book | Struct holding title, author, checkout status |
| HashNode | Linked list node for collision chaining |
| HashTable | Core class with CRUD + file I/O methods |
| hashFunction() | Maps book ID to bucket index |
| librarySystem() | Main menu loop and user interaction |
Hash Tables • Collision Resolution • Linked Lists
File I/O • Memory Management • CRUD Design Patterns
STL Containers • Input Validation • Destructor Design
| Concept | Implementation Detail |
|---|---|
| Chaining | Linked lists at each bucket for collision handling |
| Hash Function | Multiplicative hashing: (key × 31) % 100 |
| Memory Safety | Destructor traverses all chains to delete nodes |
| Duplicate Prevention | Pre-insertion check for existing IDs |
| Persistent Storage | Load on startup, save on exit |
50 Classic Books | IDs: 101-150 | Authors: 35+
Includes works by Orwell, Tolkien, Hemingway, Austen, and more
Part of the DSA Projects Roadmap — Phase 1, Project #5
📚 Happy Reading! 🚀