C Separate Chaining

Intermediate
6 min read
Updated: Nov 2026
1 example

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.

Remember
struct Node { int key; struct Node *next; };
struct Node *bucket[M];
/* insert: n->next = bucket[i]; bucket[i] = n; */

Cheat Sheet

Keep these facts handy while you work with Separate Chaining in C.

TopicDetail
InsertO(1) at head
SearchO(chain length)
CapacityCan exceed bucket count
MemoryOne pointer per entry

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Open Addressing.

Example 1 — Print the buckets

Keys 10, 15, 22, 27, 33, 40 in five buckets.

C
#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;
}

How It Works

  1. Inserting at the head reverses arrival order within a bucket.
  2. Bucket 0 received 10, 15, 40, so it reads 40, 15, 10.
  3. 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.

Key Takeaways

  • Each bucket is a list.
  • Insert at head.
  • Cost grows with load factor.

One line: In separate chaining each bucket holds a linked list of all keys that hash there.

Frequently Asked Questions

A collision strategy where every table slot holds a linked list of entries that hash to it.
O(1 + n/m), where n is the number of entries and m the number of buckets.
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.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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