Home C Hash Tables Hash Table Deletion in C Hash Table Deletion in C Definition
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.
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; Reference
Cheat Sheet Keep these facts handy while you work with Hash Table Deletion in C in C.
Topic Detail Chaining Unlink and free Open addressing Use a tombstone Time O(1) average Return Status: removed or absent
Code
C Examples Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Collision Handling .
1. Delete from the middle of a chain Example 1 — Delete from the middle of a chain Keys 1, 8, and 15 all land in bucket 1 of 7.
#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;
} chain before: 15 8 1
deleted 8: yes
chain after: 15 1
deleted 99: no How It Works New entries go to the front, so the chain reads 15, 8, 1. pp points at the link that references the entry to delete.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.
Remember
Key Takeaways 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 How do you delete from a hash table with chaining? Hash the key, find the entry in the bucket's linked list, unlink it, and free it.
What is a tombstone? A special marker left in an open-addressing slot after deletion so that searches keep probing past it.
What should I learn after Hash Table Deletion in C? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion