int main() // Create dictionary HashTable *dict = create_dict(TABLE_SIZE);// Insert entries put(dict, "apple", 10); put(dict, "banana", 20); put(dict, "cherry", 30); // Lookup values int found; int val = get(dict, "banana", &found); if (found) printf("banana -> %d\n", val); // Update existing key put(dict, "apple", 99); // Delete a key delete_key(dict, "cherry"); // Display all entries printf("\nDictionary contents:\n"); for (int i = 0; i < dict->size; i++) Entry *curr = dict->buckets[i]; if (curr) printf("Bucket %d: ", i); while (curr) printf("(%s:%d) ", curr->key, curr->value); curr = curr->next; printf("\n"); // Cleanup free_dict(dict); return 0;
A poor hash function leads to many collisions, degrading performance to O(n). A good hash function for a dictionary must have:
Below is a skeletal C implementation demonstrating the core operations:
#include <stdio.h> #include <stdlib.h> #include <string.h>#define TABLE_SIZE 10007 // A prime number c program to implement dictionary using hashing algorithms
typedef struct Entry char *key; int value; struct Entry *next; Entry;
Entry *table[TABLE_SIZE];
unsigned long hash(char *str) unsigned long hash = 5381; int c; while ((c = *str++)) hash = ((hash << 5) + hash) + c; return hash % TABLE_SIZE;
void insert(char *key, int value) unsigned long idx = hash(key); Entry *newEntry = malloc(sizeof(Entry)); newEntry->key = strdup(key); newEntry->value = value; newEntry->next = table[idx]; table[idx] = newEntry; // Prepend to chain int main() // Create dictionary HashTable *dict =
int *search(char *key) unsigned long idx = hash(key); Entry *current = table[idx]; while (current) if (strcmp(current->key, key) == 0) return ¤t->value; current = current->next; return NULL;
void delete(char *key) unsigned long idx = hash(key); Entry *current = table[idx]; Entry *prev = NULL; while (current) if (strcmp(current->key, key) == 0) if (prev) prev->next = current->next; else table[idx] = current->next; free(current->key); free(current); return; prev = current; current = current->next;
This implementation uses a fixed static table size. For production use, dynamic resizing (rehashing) is essential to maintain (O(1)) performance as the dictionary grows. A poor hash function leads to many collisions
Let's analyze the time complexities:
| Operation | Average Case | Worst Case (All Collisions) | |-----------|-------------|-------------------------------| | Insert | O(1) | O(n) | | Search | O(1) | O(n) | | Delete | O(1) | O(n) | | Resize | O(n) amortized | O(n) |
The worst case occurs when all keys hash to the same bucket.
| Pitfall | Solution | |---------|----------| | Forgetting to handle duplicate keys | Always traverse the chain before insertion | | Memory leak on deletion | Free both key and value strings | | Integer overflow in hash | Use unsigned long and modulo | | Poor hash distribution | Test with real data; switch to SDBM or Murmur | | Resizing too often | Increase threshold to 0.75 or 0.8 |
REACH NEW HEIGHTS

© 2026 TalentLaunch