Capstone: Hash Table in C
A hash table with separate chaining distills the course into one project: string keys hashed (djb2: hash = hash * 33 + c) modulo bucket count, collisions chained through linked-list nodes, with strdup'd keys, load-factor-triggered rehashing, and full create/insert/find/delete/destroy lifecycle.
It exercises strings, pointers, structs, dynamic memory, ownership (who frees keys?), and function pointers (custom hash/compare) simultaneously — the data structure behind interpreters' symbol tables and countless caches, built here from raw C with no library help.
Hash to a bucket
djb2 loops hash = hash * 33 + (unsigned char)c over key bytes — simple, fast, well-distributed for strings.
Chain collisions
Each bucket heads a linked list of entries — collisions append, lookups walk and strcmp.
Own the keys
strdup keys on insert, free on delete/destroy — ownership documented once, honored everywhere.
Rehash on load
Past ~0.75 load factor, double buckets and reinsert all — amortized O(1) preserved as data grows.
#include <stdio.h>#include <stdlib.h>#include <string.h> unsigned long hash_str(const char *s) { unsigned long h = 5381; while (*s) h = h * 33 + (unsigned char)*s++; return h;} int main(void) { printf("%lu\n", hash_str("a") % 8); printf("%lu\n", hash_str("b") % 8); return 0;}Degrades toward O(n) buckets under pathological hashing or overload.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.