VOOZH about

URL: https://www.geeksforgeeks.org/dsa/implementation-of-hash-table-in-c-using-separate-chaining/

⇱ Implementation of Hash Table in C/C++ using Separate Chaining - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implementation of Hash Table in C/C++ using Separate Chaining

Last Updated : 28 May, 2026

Hashing is a technique used to map a large amount of data to a smaller, fixed-size value using a hash function.

  • The process is irreversible - the original data cannot be reconstructed from the hash.
  • Since large data is mapped into a smaller space, collisions can occur (different inputs producing the same hash).
  • Collisions are common and unavoidable when the input set is larger than the output range.

Some Examples of Hash Functions

  • key % number of buckets
  • ASCII value of character * PrimeNumberx. Where x = 1, 2, 3....n
  • You can make your own hash function but it should be a good hash function that gives less number of collisions.
👁 Image
Components of Hashing

Bucket Index

The value returned by the Hash function is the bucket index for a key in a separate chaining method. Each index in the array is called a bucket as it is a bucket of a linked list.

Rehashing

Rehashing is a technique used in hash tables to reduce collisions when the number of elements increases. In rehashing, a new hash table with larger capacity (usually double the previous size) is created, and all existing elements are reinserted using the updated hash function.

Note: Rehashing generally occurs when the load factor becomes greater than 0.5.

Steps in Rehashing

  • Double the size of the hash table.
  • Reinsert all existing elements into the new table using the hash function.
  • Delete the old table and replace it with the new one.

Load Factor Formula

Load Factor = Number of Elements / Total Number of Buckets

Collision

Collision is the situation when the bucket index is not empty. It means that a linked list head is present at that bucket index. We have two or more values that map to the same bucket index.

Major Functions in our Program

  • Insertion
  • Search
  • Hash Function
  • Delete
  • Rehashing
👁 Image
Hash Map

Implementation without Rehashing:


Output
Manish
Anjali
Vartika
Mayank
GeeksforGeeks
Oops! No data found.


After deletion : 
Oops! No data found.

Explanation:

  • Insertion: Inserts the key-value pair at the head of the linked list at the calculated bucket index and increments numOfElements.
  • hashFunction: Calculates the bucket index using ASCII values of characters multiplied by powers of the prime number 31.
  • Deletion: Removes the key-value pair associated with the given key from the hash table. After successful deletion, numOfElements is decremented.
  • Search: Searches for the value associated with the given key in the hash table.

This implementation does not use rehashing and works with a fixed-size array of linked lists. In the given example, both keys and values are strings.

Time Complexity and Space Complexity:

The time complexity of hash table insertion and deletion operations is O(1) on average. There is some mathematical calculation that proves it.

  • Time Complexity of Insertion: In the average case it is constant. In the worst case, it is linear.
  • Time Complexity of Search: In the average case it is constant. In the worst case, it is linear.
  • Time Complexity of Deletion: In average cases it is constant. In the worst case, it is linear.
  • Space Complexity: O(n) as it has n number of elements.

Related Article: Separate Chaining Collision Handling Technique in Hashing.

Comment