📖 Scenario: Imagine you are building a simple phone book application that stores names and phone numbers. To quickly find a phone number by name, you use a hash table. Sometimes, two names might get the same spot in the table (a collision). To handle this, you will use chaining, which means each spot in the table holds a list of entries.
🎯 Goal: You will create a hash table with chaining to store and handle collisions of phone book entries. You will add entries and then print the table to see how collisions are handled.
📋 What You'll Learn
Create a hash table with 3 buckets using an array of linked lists
Define a struct for phone book entries with name and phone number
Implement a simple hash function that maps names to bucket indices
Insert entries into the hash table using chaining for collisions
Print the hash table showing all entries in each bucket
💡 Why This Matters
🌍 Real World
Hash tables with chaining are used in many applications like databases, caches, and phone books to quickly find data even when collisions happen.
💼 Career
Understanding collision handling is important for software engineers working on efficient data storage, retrieval systems, and performance optimization.
Progress0 / 4 steps
1
Create the phone book entry struct and hash table array
Define a struct called Entry with char name[20], char phone[15], and a pointer next to another Entry. Then create an array called hashTable of size 3 that holds pointers to Entry.
DSA C
Hint
Use typedef struct Entry to define the entry. Initialize hashTable with NULLs.
2
Create a hash function to map names to bucket indices
Write a function called hash that takes a char* called name and returns an int. The function should sum the ASCII values of the characters in name and return the sum modulo 3.
DSA C
Hint
Loop through each character in name, add its ASCII value to sum, then return sum % 3.
3
Insert entries into the hash table using chaining
Write a function called insert that takes char* name and char* phone. It should create a new Entry, copy the name and phone, compute the bucket index using hash, and insert the new entry at the start of the linked list in hashTable at that index.
DSA C
Hint
Create a new entry with malloc, copy the strings, find the bucket with hash, and insert at the front of the list.
4
Print the hash table showing all entries in each bucket
Write a function called printHashTable that loops over each bucket index from 0 to 2. For each bucket, print Bucket i: and then traverse the linked list printing each entry as name -> phone. Finally, call insert to add these entries: ("Alice", "123"), ("Bob", "456"), ("Clara", "789"), ("David", "321"), and then call printHashTable.
DSA C
Hint
Loop through each bucket, print the bucket number, then print each entry in the linked list. Insert the given entries before printing.
Practice
(1/5)
1. What is the main idea behind collision handling using chaining in hash tables?
easy
A. Store all elements that hash to the same index in a linked list at that index
B. Replace the existing element with the new one when a collision occurs
C. Use a secondary hash function to find a new index
D. Resize the hash table immediately when a collision happens
Solution
Step 1: Understand collision in hash tables
When two keys hash to the same index, a collision occurs.
Step 2: Explain chaining method
Chaining stores all collided elements in a linked list at that index, allowing multiple elements per slot.
Final Answer:
Store all elements that hash to the same index in a linked list at that index -> Option A
Quick Check:
Chaining = linked list at index [OK]
Hint: Chaining means linked list at each hash index [OK]
Common Mistakes:
Confusing chaining with open addressing
Thinking collisions overwrite existing data
Believing resizing happens on every collision
2. Which of the following is the correct way to declare a node structure for chaining in C?
easy
A. typedef struct Node { int key; Node* next; } Node;
B. struct Node { int key; int next; };
C. typedef struct Node { int key; struct Node* next; } Node;
D. struct Node { int key; struct Node next; };
Solution
Step 1: Check correct pointer usage in struct
In C, to create a linked list node, the next pointer must be a pointer to the same struct type.
Step 2: Validate typedef syntax
typedef struct Node { int key; struct Node* next; } Node; correctly uses struct Node* next; and typedefs the struct as Node.
Final Answer:
typedef struct Node { int key; struct Node* next; } Node; -> Option C
Quick Check:
Pointer to struct Node is struct Node* next [OK]
Hint: Use struct Node* for next pointer in C structs [OK]
Common Mistakes:
Using struct Node next instead of pointer
Missing typedef keyword or incorrect syntax
Declaring next as int instead of pointer
3. Consider the following code snippet for inserting keys into a hash table using chaining. What will be the output after inserting keys 5, 15, and 25 if the hash function is key % 10?
B. newNode is a local variable and will be lost after function ends
C. next pointer is not assigned properly
D. Hash function is incorrect
Solution
Step 1: Identify variable scope issue
newNode is declared as a local variable on the stack inside insert function.
Step 2: Explain why this causes a bug
After function returns, newNode memory is invalid, so hashTable points to invalid memory.
Final Answer:
newNode is a local variable and will be lost after function ends -> Option B
Quick Check:
Local variable address stored = invalid pointer [OK]
Hint: Use malloc for nodes, not local variables [OK]
Common Mistakes:
Ignoring variable lifetime
Thinking hash function is wrong
Assuming next pointer assignment is incorrect
5. You have a hash table of size 7 using chaining. You insert keys: 10, 21, 17, 14, 28. The hash function is key % 7. After all insertions, which index will have the longest chain and what will be the order of keys in that chain if new nodes are inserted at the head?
Step 2: Group keys by index and order insertion at head
Index 0 keys inserted in order: 21, then 14, then 28 (last inserted at head). So chain at index 0 is 28 -> 14 -> 21 -> NULL. Index 3 keys are 10 and 17.
Final Answer:
Index 0: 28 -> 14 -> 21 -> NULL -> Option A
Quick Check:
Longest chain at index 0 with keys inserted at head [OK]
Hint: Calculate key % size, insert new nodes at head [OK]
Common Mistakes:
Miscomputing hash indexes
Mixing up insertion order in chains
Choosing wrong index for longest chain
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.