Simple "sum of ASCII" functions lead to many collisions. Algorithms like djb2 or MurmurHash are much better for real-world data.
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;
To handle this, we use collision resolution techniques. The two most common are:
free(ht->buckets); free(ht);
ht_remove(dict, "orange"); printf("contains orange? %s\n", ht_contains(dict, "orange") ? "yes" : "no"); c program to implement dictionary using hashing algorithms
Below is the complete, self-contained C code demonstrating how to initialize the dictionary, insert data, look up values, delete keys, and free the allocated memory.
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 hash function mathematically maps arbitrary data (like a string) to a fixed-size integer, which corresponds to an index in the hash table array. A good hash function should:
// Create a new item struct DictionaryItem* newItem = (struct DictionaryItem*)malloc(sizeof(struct DictionaryItem)); newItem->key = key; newItem->value = value; newItem->next = NULL; Simple "sum of ASCII" functions lead to many collisions
val = get(myDict, "grape"); if (!val) printf("Get 'grape': Key not found.\n");
// 2. The Dictionary Structure typedef struct Dictionary KeyValue **table; // Array of pointers to KeyValue nodes int size; int count; // Number of elements currently stored Dictionary;
Index [5]: (banana: 20) -> (apple: 15) -> NULL
The fundamental idea is to use a hash function to map a "Key" (e.g., a string) to an "Index" in an array. This array is often called a . This implementation uses a fixed static table size
printf("=== Dictionary Implementation using Hashing in C ===\n\n");
// Case B: Key does not exist. Create a new node. KeyValue *new_node = (KeyValue*)malloc(sizeof(KeyValue)); new_node->key = duplicate_string(key); new_node->value = value;
return -1; // Key not found
current = current->next;