C Program To Implement Dictionary Using Hashing Algorithms

// Destroy the entire hash table void destroy_table(HashTable *table) if (!table) return; for (int i = 0; i < table->size; i++) Entry *current = table->buckets[i]; while (current) Entry *temp = current; current = current->next; free_entry(temp);

/* Key not found – insert new node at the head of the list */ Node *new_node = create_node(key, value); new_node->next = d->table[idx]; d->table[idx] = new_node; d->count++;

For debugging and demonstration, we can print all key‑value pairs:

// Deleting deleteItem(&ht, 2); deleteItem(&ht, 99); // Non-existent key c program to implement dictionary using hashing algorithms

printf("Inserted ('%s', %d) at index %u\n", key, value, idx);

Enter your choice: 4

return hashTable;

// 5. Cleanup 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);

A hash function h(key) transforms the key into an integer (the hash code). This integer is then reduced modulo the size of the underlying array to obtain an array index:

int main() struct HashTable ht; initHashTable(&ht); Since C lacks a built-in dictionary type, you

To implement a dictionary in C using hashing, you essentially build a that maps string keys to specific values . Since C lacks a built-in dictionary type, you must manage memory and collisions manually. 1. Core Components

You can test the program with various scenarios:

A hash function h(key) maps a key (often a string or integer) to an integer in the range [0, size-1] , where size is the capacity of the hash table. for (int i = 0