C Program To Implement Dictionary Using Hashing Algorithms -
You are now equipped to integrate a dictionary into your own C projects – whether it is a configuration parser, a symbol table for an interpreter, or a fast in‑memory cache. Experiment with the code, modify the hash function, and watch the behaviour. Happy coding!
#define TABLE_SIZE 10007 // Prime number for better distribution
// Dictionary entry (key-value pair) typedef struct Entry char *key; int value; struct Entry *next; Entry;
In C, a dictionary is typically implemented using a , which maps unique keys to specific values. Since C doesn't have a built-in dictionary type like Python or Java, you must build one using an array and a hashing function. 1. Define the Data Structures c program to implement dictionary using hashing algorithms
Insert or update a key-value pair
#include #include #include #define TABLE_SIZE 10 // Node structure for Separate Chaining struct Node char key[50]; char value[100]; struct Node* next; ; // Hash Table structure struct HashTable struct Node* bucket[TABLE_SIZE]; ; // A simple Hash Function (DJB2 algorithm style) unsigned int hash(char* key) unsigned int hashValue = 0; for (int i = 0; key[i] != '\0'; i++) hashValue = (hashValue << 5) + key[i]; return hashValue % TABLE_SIZE; // Create a new node struct Node* createNode(char* key, char* value) struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); strcpy(newNode->key, key); strcpy(newNode->value, value); newNode->next = NULL; return newNode; // Insert into Dictionary void insert(struct HashTable* ht, char* key, char* value) unsigned int index = hash(key); struct Node* newNode = createNode(key, value); if (ht->bucket[index] == NULL) ht->bucket[index] = newNode; else // Handle collision via Separate Chaining (insert at head) newNode->next = ht->bucket[index]; ht->bucket[index] = newNode; printf("Inserted: [%s : %s]\n", key, value); // Search for a key void search(struct HashTable* ht, char* key) unsigned int index = hash(key); struct Node* temp = ht->bucket[index]; while (temp != NULL) if (strcmp(temp->key, key) == 0) printf("Found: %s -> %s\n", key, temp->value); return; temp = temp->next; printf("Error: Key '%s' not found.\n", key); // Main Driver Code int main() struct HashTable ht; // Initialize buckets to NULL for (int i = 0; i < TABLE_SIZE; i++) ht.bucket[i] = NULL; insert(&ht, "C", "A general-purpose programming language."); insert(&ht, "Hash", "A function that converts data into a fixed-size value."); insert(&ht, "Pointer", "A variable that stores a memory address."); printf("\n--- Dictionary Search ---\n"); search(&ht, "C"); search(&ht, "Python"); // Not inserted return 0; Use code with caution. 3. How the Code Works
// Dictionary structure typedef struct Entry** buckets; int size; // number of buckets (table size) int count; // number of key-value pairs stored Dictionary; You are now equipped to integrate a dictionary
is highly recommended due to its speed and low collision rate. It works by performing a series of XOR and multiply operations on each byte of the key. FNV_OFFSET 14695981039346656037ULL 1099511628211ULL hash = FNV_OFFSET; * p = key; *p; p++) hash ^= ( )(*p); hash *= FNV_PRIME; Use code with caution. Copied to clipboard
#include <stdio.h> #include <stdlib.h> #include <string.h>
// A key-value pair node typedef struct Entry char* key; int value; struct Entry* next; Entry; #define TABLE_SIZE 10007 // Prime number for better
void destroy_table(HashTable *ht) if (!ht) return; for (size_t i = 0; i < ht->capacity; ++i) Node *cur = ht->buckets[i]; while (cur) Node *next = cur->next; free(cur->key); free(cur); cur = next;
An algorithm that converts a string key into an integer index matching the array size.
new_node->value = value; new_node->next = NULL; return new_node;
if (!dict->buckets) free(dict); return NULL;
No hashing algorithm is completely immune to collisions (when two unique strings generate the identical hash index). This implementation uses . When a collision occurs, the items are stored sequentially in a singly linked list at that specific array bucket. 2. Optimization with Prime Array Sizes



