Bird
0
0
DSA Cprogramming~30 mins

Hash Table Concept and Hash Functions in DSA C - Build from Scratch

Choose your learning style9 modes available
Hash Table Concept and Hash Functions
📖 Scenario: Imagine you are organizing a small library. You want to quickly find books by their unique ID numbers. To do this, you decide to use a hash table, which helps you store and find books fast using a special function called a hash function.
🎯 Goal: You will create a simple hash table in C that stores book IDs and their titles. You will write a hash function to find the correct place to store each book. Finally, you will print the stored books to see how the hash table works.
📋 What You'll Learn
Create an array to represent the hash table with 10 slots
Write a hash function that returns the index by taking the book ID modulo 10
Store three books with given IDs and titles in the hash table
Print the contents of the hash table showing index and stored book title
💡 Why This Matters
🌍 Real World
Hash tables are used in many applications like databases, caches, and dictionaries to quickly find data using keys.
💼 Career
Understanding hash tables is important for software developers and engineers to optimize data storage and retrieval in real-world systems.
Progress0 / 4 steps
1
Create the hash table array
Create a hash table array called hashTable of size 10 that can store pointers to strings (book titles). Initialize all elements to NULL.
DSA C
Hint

Use char *hashTable[10] = {NULL}; to create an array of 10 string pointers all set to NULL.

2
Write the hash function
Write a function called hashFunction that takes an integer bookID and returns bookID % 10 as the index.
DSA C
Hint

The hash function should return the remainder when bookID is divided by 10.

3
Store books in the hash table
Store the following books in the hashTable using the hashFunction to find the index:
Book ID 21 with title "C Programming"
Book ID 32 with title "Data Structures"
Book ID 43 with title "Algorithms"
DSA C
Hint

Use the hash function to get the index and assign the book title to that index in hashTable.

4
Print the hash table contents
Add a main function that stores the books as before and then prints the index and book title for each non-NULL slot in hashTable using a for loop.
DSA C
Hint

Use a for loop from 0 to 9. Inside, check if hashTable[i] is not NULL. If so, print the index and the book title.