C Program To Implement Dictionary Using Hashing Algorithms -

int main() // Create a dictionary with 10007 buckets HashTable *dict = create_hash_table(TABLE_SIZE); if (!dict) printf("Failed to create dictionary\n"); return 1;

value = search(dict, "kiwi", &found); if (found) printf("kiwi -> %d\n", value); else printf("kiwi not found\n");

// Insert or update a key-value pair void insert(HashTable *table, const char *key, int value) if (!table // Search: returns the value if key exists, or -1 if not found // (In production, use a status flag or pointer to indicate failure) int search(HashTable *table, const char *key, int *found) 4.4 Delete a Key-Value Pair // 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"); c program to implement dictionary using hashing algorithms

#include <pthread.h> typedef struct // ... existing fields ... pthread_mutex_t lock; // Global lock HashTable;

void rehash(HashTable *table) if (!table) return; int old_size = table->size; KeyValuePair **old_buckets = table->buckets; int main() // Create a dictionary with 10007

printf("==========================================\n"); // Free all memory used by the hash table void destroy_hash_table(HashTable *table) if (!table) return; for (int i = 0; i < table->size; i++) KeyValuePair *current = table->buckets[i]; while (current) KeyValuePair *temp = current; current = current->next; free(temp->key); free(temp);

return hash;

display(dict);

Leave a Reply