Hash Tables & Graphs
Implement hash tables with collision handling and graph data structures with traversal algorithms in C.
50 min•By Priygop Team•Last updated: Feb 2026
Hash Tables
- Hash Function: Maps keys to array indices — hash(key) = key % TABLE_SIZE. Good hash functions distribute keys uniformly. Common: djb2, FNV-1a for string keys
- Chaining: Each array slot is a linked list — collisions add to the list. Simple to implement, handles high load factors well. Average O(1), worst O(n)
- Open Addressing: Collisions probe for next empty slot — Linear probing (i+1, i+2...), Quadratic probing (i+1², i+2²...), Double hashing (i + j*hash2(key))
- Load Factor: Number of elements / table size. When load factor > 0.75, resize (double table size, rehash all elements). Keeps operations close to O(1)
- Applications: Symbol tables (compilers), caches, database indexing, counting frequencies, implementing sets, detecting duplicates in O(n) time
- Implementation in C: struct HashNode { char* key; int value; struct HashNode* next; }; Array of HashNode pointers. malloc/free for dynamic chains
Try It Yourself: Linked List
Try It Yourself: Linked ListC
C Editor
✓ ValidTab = 2 spaces
C|49 lines|951 chars|✓ Valid syntax
UTF-8