C Program To Implement Dictionary Using Hashing Algorithms [ Cross-Platform ]
#include <pthread.h> typedef struct // ... existing fields ... pthread_mutex_t lock; // Global lock HashTable;
#include <stdio.h> #include <stdlib.h> #include <string.h> // [Include all the functions defined above: hash_djb2, create_hash_table, // insert, search, delete_key, display, destroy_hash_table]
// Re-insert all old entries for (int i = 0; i < old_size; i++) KeyValuePair *current = old_buckets[i]; while (current) insert(table, current->key, current->value); KeyValuePair *temp = current; current = current->next; free(temp->key); free(temp); c program to implement dictionary using hashing algorithms
// Insert some key-value pairs printf("Inserting entries...\n"); insert(dict, "apple", 10); insert(dict, "banana", 20); insert(dict, "orange", 30); insert(dict, "grape", 40); insert(dict, "apple", 99); // Update existing key
printf("=== Dictionary Implementation using Hashing in C ===\n\n"); #include <pthread
if (found) *found = 0; return -1; // Delete a key from the dictionary int delete_key(HashTable *table, const char *key) if (!table 4.5 Display the Dictionary // Display all key-value pairs (for debugging) void display(HashTable *table) if (!table) return; printf("\n=== Dictionary Contents (Total: %d entries) ===\n", table->count); for (int i = 0; i < table->size; i++) if (table->buckets[i]) printf("Bucket[%d]: ", i); KeyValuePair *current = table->buckets[i]; while (current) printf("(%s -> %d) ", current->key, current->value); current = current->next; printf("\n");
// Search for keys printf("\nSearching for keys:\n"); int found; int value = search(dict, "banana", &found); if (found) printf("banana -> %d\n", value); else printf("banana not found\n"); // Global lock HashTable
// Allocate memory for the bucket array table->buckets = (KeyValuePair**)calloc(size, sizeof(KeyValuePair*)); if (!table->buckets) free(table); return NULL;

