C Program To Implement Dictionary Using Hashing Algorithms Link – Must Watch

// Delete a key from the dictionary int delete_key(HashTable *table, const char *key)

current = current->next;

dict_destroy(dict); return 0;

The same key must always produce the same hash index.

current = current->next;

This article presented a complete, working C program to implement a dictionary using hashing algorithms with separate chaining. Hashing is a powerful technique that provides near‑constant time performance for insert, search, and delete operations, making it the go‑to choice for many real‑world dictionary applications.

typedef struct Node char *key; int value; struct Node *next; Node; c program to implement dictionary using hashing algorithms

If you want to take this implementation further, let me know if you would like me to:

return d;

#include #include #include #define TABLE_SIZE 10 // Structure for the Key-Value pair (Linked List Node) typedef struct KeyValue char* key; char* value; struct KeyValue* next; KeyValue; // The Hash Table structure typedef struct Dictionary KeyValue** table; int size; Dictionary; // 1. Hash Function (DJB2 Algorithm) // This is a widely used, efficient string hashing algorithm. unsigned int hash(const char* key) unsigned long int hashval = 5381; int c; while ((c = *key++)) hashval = ((hashval << 5) + hashval) + c; // hash * 33 + c return hashval % TABLE_SIZE; // 2. Create a new Key-Value Node KeyValue* create_node(const char* key, const char* value) KeyValue* new_node = (KeyValue*)malloc(sizeof(KeyValue)); new_node->key = strdup(key); new_node->value = strdup(value); new_node->next = NULL; return new_node; // 3. Initialize the Dictionary Dictionary* create_dictionary() Dictionary* dict = (Dictionary*)malloc(sizeof(Dictionary)); dict->size = TABLE_SIZE; dict->table = (KeyValue**)calloc(dict->size, sizeof(KeyValue*)); return dict; // 4. Insert a Key-Value pair into the Dictionary void insert(Dictionary* dict, const char* key, const char* value) unsigned int index = hash(key); KeyValue* current = dict->table[index]; // Check if key already exists, update value if it does while (current != NULL) if (strcmp(current->key, key) == 0) free(current->value); current->value = strdup(value); return; current = current->next; // Key doesn't exist, insert new node at the beginning of the chain KeyValue* new_node = create_node(key, value); new_node->next = dict->table[index]; dict->table[index] = new_node; // 5. Search for a Value using its Key char* search(Dictionary* dict, const char* key) unsigned int index = hash(key); KeyValue* current = dict->table[index]; // Traverse the linked list at the hashed index while (current != NULL) if (strcmp(current->key, key) == 0) return current->value; // Key found current = current->next; return NULL; // Key not found // 6. Delete a Key-Value pair from the Dictionary void delete(Dictionary* dict, const char* key) unsigned int index = hash(key); KeyValue* current = dict->table[index]; KeyValue* prev = NULL; while (current != NULL) if (strcmp(current->key, key) == 0) if (prev == NULL) // Node to delete is the first in the chain dict->table[index] = current->next; else // Node to delete is in the middle or end prev->next = current->next; // Free memory free(current->key); free(current->value); free(current); printf("Successfully deleted key: %s\n", key); return; prev = current; current = current->next; printf("Key not found: %s\n", key); // 7. Free the entire Dictionary from memory void free_dictionary(Dictionary* dict) for (int i = 0; i < dict->size; i++) KeyValue* current = dict->table[i]; while (current != NULL) KeyValue* temp = current; current = current->next; free(temp->key); free(temp->value); free(temp); free(dict->table); free(dict); // 8. Main function to test the Dictionary int main() Dictionary* dict = create_dictionary(); // Insert data insert(dict, "apple", "A sweet red or green fruit."); insert(dict, "banana", "A long curved fruit with a yellow skin."); insert(dict, "cherry", "A small, round stone fruit."); // Testing duplicate insertion (updating value) insert(dict, "apple", "An updated definition for the apple."); // Search data printf("Searching for 'apple': %s\n", search(dict, "apple")); printf("Searching for 'banana': %s\n", search(dict, "banana")); printf("Searching for 'grape': %s\n", search(dict, "grape")); // Should be NULL // Delete data delete(dict, "banana"); printf("Searching for 'banana' after deletion: %s\n", search(dict, "banana")); // Clean up memory free_dictionary(dict); return 0; Use code with caution. How to Compile and Run

Prime numbers tend to minimize collisions in hash tables. If you expect a massive dataset, initializing the hash table with a large prime number (e.g., 65521 ) is a good baseline. Conclusion // Delete a key from the dictionary int