C Program To Implement Dictionary Using Hashing Algorithms Verified
A dictionary requires three primary functions: insert , search , and delete .
O(n + m) where m is table size. Each entry stores two strings plus a pointer overhead.
A poor hash function leads to many , degrading performance to O(n). A good hash function for a dictionary must have:
/*============================================================== * DICTIONARY USING HASHING WITH SEPARATE CHAINING * ------------------------------------------------------------ * Implements a dictionary (key-value store) where keys are * strings and values are integers. Collisions are resolved * using separate chaining with singly linked lists. *==============================================================*/ c program to implement dictionary using hashing algorithms
A good hash function for a string (or integer) key should:
#include <stdio.h> #include <stdlib.h> #include <string.h>
/* Structure for a node in the linked list (key-value pair) */ typedef struct Node char key; / Dynamically allocated key string / int value; / Associated integer value */ struct Node next; / Next node in the chain */ Node; A dictionary requires three primary functions: insert ,
Hashing transforms a variable-length key into a fixed-size integer index. This index determines where the corresponding value is stored in an array. Hash Functions
#include #include #include #define SIZE 20 // Define the entry structure struct Entry char* key; char* value; struct Entry* next; ; // The Hash Table (Array of pointers) struct Entry* hashTable[SIZE]; // Simple hash function (Sum of ASCII values) unsigned int hash(char* key) unsigned int hashVal = 0; while (*key) hashVal += *key++; return hashVal % SIZE; // Insert or update a key-value pair void insert(char* key, char* value) int index = hash(key); struct Entry* current = hashTable[index]; // Check if key already exists to update value while (current != NULL) if (strcmp(current->key, key) == 0) free(current->value); current->value = strdup(value); return; current = current->next; // Create new entry if not found struct Entry* newEntry = malloc(sizeof(struct Entry)); newEntry->key = strdup(key); newEntry->value = strdup(value); newEntry->next = hashTable[index]; // Insert at head of list hashTable[index] = newEntry; // Search for a value by key char* search(char* key) int index = hash(key); struct Entry* current = hashTable[index]; while (current != NULL) if (strcmp(current->key, key) == 0) return current->value; current = current->next; return "Not Found"; int main() insert("apple", "A red fruit"); insert("book", "A set of printed pages"); printf("apple: %s\n", search("apple")); printf("book: %s\n", search("book")); printf("grape: %s\n", search("grape")); return 0; Use code with caution. Copied to clipboard Key Operations & Performance Dictionary hashing | Intro to CS - Python | Khan Academy
For strings, a simple yet effective hash function is the algorithm (created by Daniel J. Bernstein): A poor hash function leads to many ,
=== Dictionary Contents (Total: 4 entries) === Bucket[4412]: (grape -> 40) Bucket[9234]: (apple -> 99) Bucket[9876]: (orange -> 30) (banana -> 20)
Let me know how you would like to proceed with customizing this code. Share public link
The heart of a dictionary is the hash function. It takes a "key" (usually a string) and converts it into an integer index. A good hash function distributes keys uniformly across the table to minimize collisions. Collision Handling