Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Readme.md

📚 Library Management System

Two approaches to efficient book cataloging and retrieval

C++11+ Hash Table Chaining Completed

A comprehensive library management system with custom hash table and STL map implementations. Demonstrates collision handling, CRUD operations, and persistent file storage.


✨ Features

📖 CRUD Operations

  • Create — Add books with duplicate ID prevention
  • Read — Search by ID with full details
  • Update — Modify title, author, or checkout status
  • Delete — Remove books with proper memory cleanup

🔄 Advanced Features

  • Persistent Storage — Auto-save/load from text files
  • Checkout System — Track book availability
  • Collision Handling — Chaining with linked lists
  • Input Validation — Error handling for edge cases

🏗️ Architecture Overview

Hash Table Structure (Custom Implementation)

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


🛠️ Two Implementations Compared

🔗 Custom Hash Table 🗺️ STL Unordered Map

File: LMSUsingLinkedList.cpp

struct HashNode {
    int val;
    Book books;
    HashNode* next;
};

vector<HashNode*> table;

Pros:

  • ✅ Full control over implementation
  • ✅ Educational value
  • ✅ Custom hash function
  • ✅ Manual memory management

Cons:

  • ❌ More code complexity
  • ❌ Manual cleanup required

File: LMSUSingMap.cpp

unordered_map<int, Book> library;

Pros:

  • ✅ Production-ready
  • ✅ Automatic memory management
  • ✅ Optimized performance
  • ✅ Less code to maintain

Cons:

  • ❌ Less learning opportunity
  • ❌ Black-box implementation

⚡ Performance Analysis

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)


🚀 Quick Start

🔨 Compilation

# 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

💡 Usage Example

$ ./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

🎯 Interactive Demo

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

📁 File Format Specification

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)


📂 Project Structure

LibraryManagementSystem/
├── 📄 LMSUsingLinkedList.cpp    # Custom hash table (350 lines)
├── 📄 LMSUSingMap.cpp           # STL unordered_map implementation
├── 📊 library.txt               # Persistent storage (50 books)
└── 📖 Readme.md                 # This file

Key Components

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

🎓 Learning Outcomes

Hash TablesCollision ResolutionLinked Lists
File I/OMemory ManagementCRUD Design Patterns
STL ContainersInput ValidationDestructor Design

🔑 Key Concepts Demonstrated

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

📊 Dataset Statistics

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! 🚀