C Program To Implement Dictionary Using Hashing Algorithms Better ✔
The Hash Function: Converts a string (the key) into an integer index. A common choice is the djb2 algorithm because it distributes strings evenly across the table.
The Hash Table: An array of structs, where each struct holds the word (key) and its definition (value).
Collision Handling: Since different words can hash to the same index, we use Linear Probing—if a slot is taken, the program simply looks at the next available slot (index + 1). The Implementation
#include Use code with caution. Copied to clipboard Why this works Speed: In a perfect scenario, finding a word is
, meaning it takes the same amount of time regardless of how many words are in the dictionary. c program to implement dictionary using hashing algorithms
Memory: By using a fixed SIZE, we manage memory predictably, though in a real-world app, you would implement Dynamic Resizing to expand the table as it fills up.
1. Data Structures
We need two primary structures:
- Node: Represents a single key-value pair (linked list node for chaining)
- HashTable: Contains an array of buckets (each a linked list head)
#include <stdio.h> #include <stdlib.h> #include <string.h>#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; The Hash Function: Converts a string (the key)
// Hash table structure typedef struct Entry **buckets; // Array of linked list heads int size; // Number of buckets int count; // Number of stored key-value pairs HashTable;
1.1 What is a Dictionary?
A dictionary is an abstract data type that stores a collection of pairs (key, value). It supports three primary operations:
- Insert: Add a new key-value pair.
- Search: Given a key, return the associated value.
- Delete: Remove a key-value pair.
The goal is to perform each of these operations in O(1) average time complexity. Node : Represents a single key-value pair (linked
Data Structures
First, define the linked list node for the chain:
typedef struct Entry
char *key;
char *value;
struct Entry *next;
Entry;
Next, define the hash table structure:
typedef struct Dictionary
Entry **buckets; // Array of pointers to linked lists
unsigned long size; // Current number of buckets
unsigned long count; // Number of key-value pairs stored
unsigned long (*hash_func)(const char *); // Function pointer for hash algorithm
Dictionary;
Step 2: Collision Resolution – Separate Chaining
Collisions are inevitable. The two main strategies are:
- Open addressing (linear probing, quadratic probing)
- Separate chaining (linked lists)
We will implement separate chaining because it handles high load factors gracefully and is simpler to manage in C.
Initialization
HashTable* create_dict(int size) HashTable *dict = (HashTable*)malloc(sizeof(HashTable)); if (!dict) return NULL;dict->size = size; dict->count = 0; dict->buckets = (Entry**)calloc(size, sizeof(Entry*)); if (!dict->buckets) free(dict); return NULL; return dict;