Home C Hash Tables Separate Chaining C Separate Chaining Definition
A List in Every Bucket In separate chaining each bucket holds a linked list of all keys that hash there. Inserting at the head is O(1); search costs the length of one chain, about n / m on average (the load factor). The table can hold more entries than buckets, and deletion is a normal list delete.
struct Node { int key; struct Node *next; };
struct Node *bucket[M];
/* insert: n->next = bucket[i]; bucket[i] = n; */ Reference
Cheat Sheet Keep these facts handy while you work with Separate Chaining in C.
Topic Detail Insert O(1) at head Search O(chain length) Capacity Can exceed bucket count Memory One pointer per entry
Code
C Examples Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Open Addressing .
1. Print the buckets Example 1 — Print the buckets Keys 10, 15, 22, 27, 33, 40 in five buckets.
#include <stdio.h>
#include <stdlib.h>
#define M 5
struct Node { int key; struct Node *next; };
int main(void) {
struct Node *bucket[M] = {0};
int keys[] = {10, 15, 22, 27, 33, 40};
for (int i = 0; i < 6; i++) {
struct Node *n = malloc(sizeof *n);
if (!n) return 1;
n->key = keys[i];
n->next = bucket[keys[i] % M];
bucket[keys[i] % M] = n;
}
for (int i = 0; i < M; i++) {
printf("[%d]", i);
if (!bucket[i]) printf(" (empty)");
for (struct Node *n = bucket[i]; n; n = n->next)
printf(" %d%s", n->key, n->next ? " ->" : "");
printf("\n");
}
for (int i = 0; i < M; i++) {
struct Node *n = bucket[i];
while (n) { struct Node *nx = n->next; free(n); n = nx; }
}
return 0;
} [0] 40 -> 15 -> 10
[1] (empty)
[2] 27 -> 22
[3] 33
[4] (empty) How It Works Inserting at the head reverses arrival order within a bucket. Bucket 0 received 10, 15, 40, so it reads 40, 15, 10. Buckets 1 and 4 never received a key. Edge Cases & Pitfalls Check these before relying on Separate Chaining in real programs.
long Long chains A poor hash or high load makes chains long and lookups slow.
mem Per-node allocation Many small malloc calls cost time and memory.
Remember
Key Takeaways One line: In separate chaining each bucket holds a linked list of all keys that hash there.
Frequently Asked Questions What is separate chaining? A collision strategy where every table slot holds a linked list of entries that hash to it.
What is the average search time with chaining? O(1 + n/m), where n is the number of entries and m the number of buckets.
What should I learn after Separate Chaining? Continue with "C Open Addressing" next. Store keys in the table itself using linear probing. The C Hash Tables hub lists every topic in order.
Next: Open Addressing Store keys in the table itself using linear probing.
Continue → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion