Unit 5 · Lesson 318 minAcademic review pending

When a room is full, add another cot

Chaining does not fight a collision — it accepts it, and lets a slot hold more than one key by keeping a small list at every bucket.

Choose explanation

After this lesson

You should be able to

  • Insert a key into a chained hash table.
  • Search a chained hash table, including a bucket with several keys.
  • Explain how chaining's worst case relates to Unit I's linked list.

Try it before you read

Try it live

hash.ctable size 11
0
1
2
3
4
5
6
7
8
9
10
01

Every slot holds a list, not just a value

In a hostel with double rooms, a room designed for two just holds two occupants — no need to relocate anyone. Chaining applies the same idea to a hash table: each slot holds not one value but a small linked list of every key that has ever hashed there. Inserting always succeeds at the computed slot — you simply add to that slot's list.

This is Unit I's singly linked list, doing real work again, one per bucket.

Insert always succeeds — it just grows a list
struct Node { int key; struct Node *next; };
struct Node *table[TABLE_SIZE];   /* one linked-list head per slot */

void insert(int key)
{
    int index = key % TABLE_SIZE;
    struct Node *n = malloc(sizeof(struct Node));
    n->key = key;
    n->next = table[index];   /* insert-front, from Unit I Lesson 2 */
    table[index] = n;
}
02

Search means checking the bucket's whole list

Searching hashes the key to find the right bucket in one step, then walks that bucket's list comparing each entry — exactly Unit I Lesson 2's traversal, just scoped to one small list instead of the whole table.

03

The worst case is an old friend

If a poor hash function or bad luck sends every key to the same bucket, chaining degrades into a single long linked list — every lookup then costs Unit I's O(n) list traversal, exactly the cost a hash table exists to avoid. This is why Lesson 2's hash function quality matters: chaining does not fail when collisions happen, but it does lose its speed advantage if collisions happen too often.

Try it yourself

Using the widget below in chaining mode, insert 5, 16, 27, and 8, then search for 27 and for 100, noting how many entries each search must check.

Need a hint?

5, 16, and 27 all share the same remainder modulo 11 — trace that by hand first.

Check the worked solution

5 % 11 = 5, 16 % 11 = 5, 27 % 11 = 5 — all three land in bucket 5, forming a chain of three, while 8 % 11 = 8 lands alone. Searching for 27 must check up to three entries in bucket 5's list. Searching for 100 hashes to 100 % 11 = 1, finds an empty bucket immediately, and correctly reports not found without walking anything — an empty bucket ends the search instantly.

Quick check

In the worst case, where every key hashes to the same bucket, what does a chained hash table's lookup cost become?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

Collision Resolution by Chaining

Maps to course outcomes CO1, CO2, CO4.