Chaining in Hashing in C: What It Is and How It Works
chaining is a method to handle collisions in hashing by storing multiple elements in a linked list at the same hash index. When two keys hash to the same slot, chaining keeps them in a list so all can be accessed without overwriting.How It Works
Imagine you have a set of keys and you want to store them in boxes labeled by numbers. Hashing assigns each key a box number using a function. Sometimes, two keys get the same box number, causing a collision.
Chaining solves this by turning each box into a small chain (linked list) where all keys with the same box number are linked together. Instead of one key per box, you have a chain of keys. When you want to find a key, you go to its box and check each key in the chain until you find it.
Example
This example shows a simple hash table using chaining with linked lists in C. It inserts keys and prints the chains for each index.
#include <stdio.h> #include <stdlib.h> #define TABLE_SIZE 5 // Node for linked list typedef struct Node { int key; struct Node* next; } Node; // Hash function int hash(int key) { return key % TABLE_SIZE; } // Insert key into hash table void insert(Node* table[], int key) { int index = hash(key); Node* newNode = malloc(sizeof(Node)); if (newNode == NULL) { fprintf(stderr, "Memory allocation failed\n"); exit(EXIT_FAILURE); } newNode->key = key; newNode->next = table[index]; table[index] = newNode; } // Print hash table chains void printTable(Node* table[]) { for (int i = 0; i < TABLE_SIZE; i++) { printf("Index %d:", i); Node* temp = table[i]; while (temp) { printf(" -> %d", temp->key); temp = temp->next; } printf(" -> NULL\n"); } } int main() { Node* hashTable[TABLE_SIZE] = {NULL}; insert(hashTable, 12); insert(hashTable, 7); insert(hashTable, 22); insert(hashTable, 17); insert(hashTable, 5); printTable(hashTable); return 0; }
When to Use
Use chaining in hashing when you expect collisions and want a simple way to store multiple keys at the same index without losing data. It is useful in situations like symbol tables in compilers, caching, or databases where quick lookup and insertion are needed.
Chaining is preferred when the number of collisions is moderate and memory for linked lists is affordable. It keeps operations efficient and easy to implement.
Key Points
- Chaining uses linked lists to handle collisions in hash tables.
- Multiple keys with the same hash index are stored in a chain.
- It allows efficient insertion and search even with collisions.
- Simple to implement and flexible for dynamic data.