Open hashing (separate chaining)
Separate chaining: each slot holds a linked list of all keys that hash there.
Syllabus says: “Open hashing”
On this page · 13 sections
Each slot holds a LINKED LIST (chain). Collision? Just add to that list. Load factor may exceed 1.
Hostel rooms with bunk beds that can be extended: if room 6 is taken, the new student just gets another bunk in room 6.
In open hashing (separate chaining) each table slot points to a linked list of all keys that hash to it; insertion adds to the list and search scans only that list.
Learning objectives
- Insert, search and delete in a chained hash table.
- Compare chaining with open addressing.
Definition
Open hashing (separate chaining) stores, at each slot i, a linked list of every key with h(k) = i. Keys live outside the table array, so the load factor α may exceed 1.
Operations
- Insert: compute h(k); append (or prepend) a node to that chain. We append, matching most exam answers — state your choice.
- Search: walk the chain at h(k).
- Delete: unlink the node — no tombstones needed.
Worked example
| Key | Home h(k) | Chain | Stored at |
|---|---|---|---|
| 72 | 6 | append | 6 |
| 13 | 2 | append | 2 |
| 6 | 6 | append | 6 |
| 5 | 5 | append | 5 |
| 10 | 10 | append | 10 |
| 36 | 3 | append | 3 |
| 43 | 10 | append | 10 |
| 17 | 6 | append | 6 |
Final table — 0: — | 1: — | 2: 13 | 3: 36 | 4: — | 5: 5 | 6: 72 → 6 → 17 | 7: — | 8: — | 9: — | 10: 10 → 43
Now watch it run
Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶. Keys: Space play/pause, ← → step.
C implementation
hashing_chaining.ctested ✓ 100/100 cases/* Separate chaining (open hashing) — KTU PCCST303, Module 4
* h(k) = k mod m. New keys are appended at the tail of the chain.
* Input : m, then operations until EOF: I key | S key | D key
* Output: each chain as "i: k1 k2 ..." and frees all memory. */
#include <stdio.h>
#include <stdlib.h>
#define MAXM 101
struct node { int key; struct node *next; };
struct node *head[MAXM];
int m;
int search(int key) {
for (struct node *p = head[key % m]; p != NULL; p = p->next)
if (p->key == key) return 1;
return 0;
}
void insert(int key) {
if (search(key)) return; /* no duplicates */
struct node *t = malloc(sizeof *t);
if (t == NULL) { printf("Out of memory\n"); exit(1); }
t->key = key; t->next = NULL;
struct node **pp = &head[key % m];
while (*pp != NULL) pp = &(*pp)->next; /* walk to tail */
*pp = t;
}
void delete(int key) {
struct node **pp = &head[key % m];
while (*pp != NULL && (*pp)->key != key) pp = &(*pp)->next;
if (*pp != NULL) { struct node *dead = *pp; *pp = dead->next; free(dead); }
}
int main(void) {
char op; int key;
if (scanf("%d", &m) != 1 || m < 1 || m > MAXM) { printf("Invalid m\n"); return 1; }
while (scanf(" %c %d", &op, &key) == 2) {
if (key < 0) { printf("Invalid key\n"); return 1; }
if (op == 'I') insert(key);
else if (op == 'S') printf("SEARCH %d -> %s\n", key, search(key) ? "found" : "not found");
else if (op == 'D') delete(key);
}
for (int i = 0; i < m; i++) {
printf("%d:", i);
for (struct node *p = head[i]; p != NULL; p = p->next) printf(" %d", p->key);
printf("\n");
while (head[i] != NULL) { struct node *t = head[i]; head[i] = t->next; free(t); }
}
return 0;
}
Sample run · m = 11 · real output from gcc
Input (stdin)
11 I 72 I 13 I 6 I 5 I 10 I 36 I 43 I 17
Output (stdout)
0: 1: 2: 13 3: 36 4: 5: 5 6: 72 6 17 7: 8: 9: 10: 10 43
Complexity
Average O(1 + α) for search under simple uniform hashing; worst case O(n) when all keys land in one chain (see model paper Q15a, where every key hashes to bucket 2).
Chaining vs open addressing
| Chaining | Open addressing |
|---|---|
| α can exceed 1 | α ≤ 1 |
| Simple deletion | Needs tombstones |
| Extra memory for pointers | No pointers; better cache use |
| Degrades gracefully | Degrades sharply as α → 1 |
Asked in KTU exams (3)
- November 2025 · Q16a6 markssolved ✓Given the values 72,13,6,5,10,36,43 and 17 and a hash table of size 11. Show the resultant table after inserting values in the given order by using hash function h(k)=k%11 with each of the following collision strategies. i) Linear Probing ii) Quadratic Probing iii) Separate Chaining
- — 2024 · Q15a4 markssolved ✓You are given a hash table with 5 buckets (i.e., indices 0 to 4). Use open hashing (separate chaining) to insert the following set of keys: {12,42,22,52,32}. Use the division method in hashing.
- — 2024 · Q16b3 marksImplement a spell checker that uses a hash table to store a dictionary of valid words, enabling fast lookup to detect and flag misspelled words in a given input text.
Quick check — can you get them all?
Practice
Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.
Exam-ready summary
Array of linked lists; average O(1 + α); easy deletion; worst case O(n).
References
- Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — Ch. 7 (Sorting), Ch. 8 (Hashing) §8.2.4 (chaining)
- Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 3/e — Ch. 2, 6, 7, 8, 11 §11.2
Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.
Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)