Hash Table Deletion in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Removing a Key

With chaining, deleting a key is a linked-list delete in one bucket: find the entry, unlink it, and free it. A pointer to the link (struct Entry **pp) removes the head and interior nodes with the same code. With open addressing you cannot simply empty a slot; you must mark a tombstone so later probes continue past it.

Remember
struct Entry **pp = &t->bucket[slot(key)];
while (*pp) {
    if ((*pp)->key == key) {
        struct Entry *dead = *pp;
        *pp = dead->next;
        free(dead);
        return 1;
    }
    pp = &(*pp)->next;
}
return 0;

Cheat Sheet

Keep these facts handy while you work with Hash Table Deletion in C in C.

TopicDetail
ChainingUnlink and free
Open addressingUse a tombstone
TimeO(1) average
ReturnStatus: removed or absent

C Examples

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

Example 1 — Delete from the middle of a chain

Keys 1, 8, and 15 all land in bucket 1 of 7.

C
#include <stdio.h>
#include <stdlib.h>
#define BUCKETS 7

struct Entry { int key; struct Entry *next; };
struct Table { struct Entry *bucket[BUCKETS]; };

static unsigned slot(int key) { return (unsigned)key % BUCKETS; }

static void put(struct Table *t, int key) {
    struct Entry *n = malloc(sizeof *n);
    if (!n) exit(1);
    n->key = key;
    n->next = t->bucket[slot(key)];
    t->bucket[slot(key)] = n;
}

static int remove_key(struct Table *t, int key) {
    struct Entry **pp = &t->bucket[slot(key)];
    while (*pp) {
        if ((*pp)->key == key) {
            struct Entry *dead = *pp;
            *pp = dead->next;
            free(dead);
            return 1;
        }
        pp = &(*pp)->next;
    }
    return 0;
}

static void print_chain(const struct Entry *e) {
    for (; e; e = e->next) printf("%d%s", e->key, e->next ? " " : "");
    printf("\n");
}

int main(void) {
    struct Table t = {{0}};
    put(&t, 1); put(&t, 8); put(&t, 15);

    printf("chain before: ");
    print_chain(t.bucket[1]);
    printf("deleted 8: %s\n", remove_key(&t, 8) ? "yes" : "no");
    printf("chain after: ");
    print_chain(t.bucket[1]);
    printf("deleted 99: %s\n", remove_key(&t, 99) ? "yes" : "no");

    for (int i = 0; i < BUCKETS; i++) {
        struct Entry *e = t.bucket[i];
        while (e) { struct Entry *n = e->next; free(e); e = n; }
    }
    return 0;
}

How It Works

  1. New entries go to the front, so the chain reads 15, 8, 1.
  2. pp points at the link that references the entry to delete.
  3. Deleting an absent key returns 0 and changes nothing.

Edge Cases & Pitfalls

Check these before relying on Hash Table Deletion in C in real programs.

tomb

Emptying a probed slot

In open addressing, clearing a slot breaks the probe sequence for keys stored after it. Use tombstones.

uaf

Using freed entry

Do not touch dead after free.

Key Takeaways

  • Chaining: unlink and free.
  • Open addressing: tombstones.
  • Return a status.

One line: With chaining, deleting a key is a linked-list delete in one bucket: find the entry, unlink it, and free it.

Frequently Asked Questions

Hash the key, find the entry in the bucket's linked list, unlink it, and free it.
A special marker left in an open-addressing slot after deletion so that searches keep probing past it.
Continue with "C Collision Handling" next. Measure collisions and compare chaining with probing. The C Hash Tables hub lists every topic in order.

Next: Collision Handling

Measure collisions and compare chaining with 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.

12 people found this page helpful