Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Module 4 · syllabusAsked 3× in KTU papersChecked by code · teacher review pending

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
ormakkurippu ✎
Never-forget card

Each slot holds a LINKED LIST (chain). Collision? Just add to that list. Load factor may exceed 1.

Picture it

Hostel rooms with bunk beds that can be extended: if room 6 is taken, the new student just gets another bunk in room 6.

Write this in the exam

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

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

Worked example (KTU Nov 2025 Q16a iii)checked by code
KeyHome h(k)ChainStored at
726append6
132append2
66append6
55append5
1010append10
363append3
4310append10
176append6

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 ◀ ▶.

Loading visualizer…

C implementation

hashing_chaining.ctested ✓ 100/100 cases
C
/* 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

Open in the Code Lab →

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

ChainingOpen addressing
α can exceed 1α ≤ 1
Simple deletionNeeds tombstones
Extra memory for pointersNo pointers; better cache use
Degrades gracefullyDegrades sharply as α → 1

Asked in KTU exams (3)

Quick check — can you get them all?

Question 1

Q1.Average search cost with chaining, load factor α = n/m?

Question 2

Q2.Can the load factor exceed 1 with chaining?

Question 3

Q3.m = 11, keys 72, 6, 17 (all h = 6). Slot 6 holds…

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)