// Delete a key printf("\nDeleting 'orange'...\n"); if (delete_key(dict, "orange")) printf("'orange' deleted successfully.\n"); else printf("'orange' not found.\n");
// Double the size (use next prime for better distribution) int new_size = next_prime(old_size * 2); table->size = new_size; table->count = 0; table->buckets = (KeyValuePair**)calloc(new_size, sizeof(KeyValuePair*));
// Cleanup destroy_hash_table(dict);
// DJB2 hash function for strings unsigned long hash_djb2(const char *str) unsigned long hash = 5381; int c; while ((c = *str++)) hash = ((hash << 5) + hash) + c; // hash * 33 + c
// SDBM hash function unsigned long hash_sdbm(const char *str) unsigned long hash = 0; int c; while ((c = *str++)) hash = c + (hash << 6) + (hash << 16) - hash;
Deleting 'orange'... 'orange' deleted successfully. === Dictionary Contents (Total: 3 entries) === Bucket[4412]: (grape -> 40) Bucket[9234]: (apple -> 99) Bucket[9876]: (banana -> 20) Total number of key-value pairs: 3 6.1 Dynamic Resizing (Rehashing) A static hash table becomes inefficient when it fills up. The load factor α = count / size should ideally stay below 0.75. Implement rehashing:
printf("=== Dictionary Implementation using Hashing in C ===\n\n");
// Insert or update a key-value pair void insert(HashTable *table, const char *key, int value) !key) return;