Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Phonebook Management System using Hash Tables

Overview

A command-line Phonebook Management System implemented in C using a hash table with 26 buckets (A-Z) and separate chaining through linked lists to handle collisions. Contacts are stored and organized based on the first letter of their names, enabling faster lookups compared to a simple linked-list implementation.

The project supports adding, searching, updating, deleting, and displaying contacts, with persistent storage through a CSV file.

Motivation

This project was built to explore the implementation of fundamental data structures in C, particularly hash tables, linked lists, dynamic memory allocation, and file handling. Rather than using existing libraries, the goal was to implement the underlying mechanisms manually for learning purposes.

Features

  • Store contacts using a hash table

  • Collision handling using linked lists (separate chaining)

  • Search contacts by:

    • Name
    • Phone number
  • Add new contacts

  • Prevent duplicate entries

  • Update existing contact information

  • Delete contacts

  • Display all contacts

  • Display contacts starting with a particular alphabet

  • Persist contacts in a CSV file

    Additional feature:

    • Estimate the number of distinct country codes by organizing contacts into a secondary hash table based on country codes.

Data Structure Design

Primary Hash Table

The phonebook contains 26 buckets:

A -> Linked List
B -> Linked List
C -> Linked List
...
Z -> Linked List

The hash function maps a contact to a bucket based on the first letter of the contact's name.

Example:

Alice  -> Bucket A
Andrew -> Bucket A
Bob    -> Bucket B
Sarvani-> Bucket S

Collisions are handled using linked lists.

Contact Structure

typedef struct node{
    char name[1000];
    char phno[30];
    struct node* next;
} Entry;

File Storage

Contacts are stored in:

PhB.csv

Format:

Alice,+91-9876543210
Bob,+1-1234567890
Charlie,+44-987654321

The phonebook is loaded from the CSV file when the program starts and written back whenever modifications are made.

Menu Options

1. Display Phonebook
2. Display contacts starting with a particular letter
3. Find a contact by name
4. Find a contact by phone number
5. Add a contact
6. Delete a contact
7. Find number of countries
-1. Exit

How to Compile

Using GCC:

gcc phonebook.c -o phonebook

How to Run

./phonebook

Example Usage

Enter your choice : 5

Enter the name : Alice
Enter the phone number in the format +ccode-phno :
+91-9876543210

Contact added successfully.

Searching by name:

Enter your choice : 3

Enter the name : Alice

Contact found!
Alice -> +91-9876543210

Time Complexity

Operation Average Case
Insert O(1)
Search by Name O(1)
Delete O(1)
Search by Phone Number O(n)
Display All Contacts O(n)

Note: Search, insertion, and deletion are efficient because only contacts sharing the same starting letter need to be traversed. The actual performance depends on the distribution of contacts across buckets.

Concepts Demonstrated

  • Hash Tables
  • Collision Handling (Separate Chaining)
  • Linked Lists
  • Dynamic Memory Allocation
  • File Handling
  • Modular Programming in C
  • String Manipulation
  • Basic Data Persistence

Future Improvements

Possible enhancements include:

  • Better hash function using the entire name
  • Dynamic resizing of the hash table
  • Memory cleanup routines
  • Input validation and safer string handling
  • Sorting without modifying stored data
  • Case-insensitive search using portable methods
  • Support for multiple contacts with identical names
  • Import/export functionality

Author

Sarvani U N

A learning project built while exploring data structures and file handling in C.

Note: This project was created as a learning exercise and out of interest to understand hash tables, linked lists, and file handling in C. Some implementation choices were intentionally kept simple for educational purposes.

About

Hash table-based phonebook in C using separate chaining (linked lists), supporting efficient search, insertion, deletion, file persistence, and country-code analysis.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages