Program To Implement Dictionary Using Hashing Algorithms: C

You have millions of books, and people constantly ask for specific titles. If you arrange them in one long row (an ), you’d have to walk miles to find "Zebra Facts". If you use a linked list , you have to follow a trail of breadcrumbs from the first book to the last. Both are too slow. The Secret: The Hashing Ritual

For an educational C program, is often chosen because:

Next, define the hash table structure:

#include #include #include #define TABLE_SIZE 10 // Node structure for Separate Chaining struct Node char key[50]; char value[100]; struct Node* next; ; // Hash Table structure struct HashTable struct Node* bucket[TABLE_SIZE]; ; // A simple Hash Function (DJB2 algorithm style) unsigned int hash(char* key) unsigned int hashValue = 0; for (int i = 0; key[i] != '\0'; i++) hashValue = (hashValue << 5) + key[i]; return hashValue % TABLE_SIZE; // Create a new node struct Node* createNode(char* key, char* value) struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); strcpy(newNode->key, key); strcpy(newNode->value, value); newNode->next = NULL; return newNode; // Insert into Dictionary void insert(struct HashTable* ht, char* key, char* value) unsigned int index = hash(key); struct Node* newNode = createNode(key, value); if (ht->bucket[index] == NULL) ht->bucket[index] = newNode; else // Handle collision via Separate Chaining (insert at head) newNode->next = ht->bucket[index]; ht->bucket[index] = newNode; printf("Inserted: [%s : %s]\n", key, value); // Search for a key void search(struct HashTable* ht, char* key) unsigned int index = hash(key); struct Node* temp = ht->bucket[index]; while (temp != NULL) if (strcmp(temp->key, key) == 0) printf("Found: %s -> %s\n", key, temp->value); return; temp = temp->next; printf("Error: Key '%s' not found.\n", key); // Main Driver Code int main() struct HashTable ht; // Initialize buckets to NULL for (int i = 0; i < TABLE_SIZE; i++) ht.bucket[i] = NULL; insert(&ht, "C", "A general-purpose programming language."); insert(&ht, "Hash", "A function that converts data into a fixed-size value."); insert(&ht, "Pointer", "A variable that stores a memory address."); printf("\n--- Dictionary Search ---\n"); search(&ht, "C"); search(&ht, "Python"); // Not inserted return 0; Use code with caution. 3. How the Code Works c program to implement dictionary using hashing algorithms

/* Traverse the list / while (curr) if (strcmp(curr->key, key) == 0) / Found: remove the node / if (prev == NULL) / Removing the head */ d->table[idx] = curr->next; else prev->next = curr->next;

You now have a production-ready implementation that you can extend with:

unsigned long hash_sdbm(const char *str) unsigned long hash = 0; int c; while ((c = *str++)) hash = c + (hash << 6) + (hash << 16) - hash; You have millions of books, and people constantly

void free_dictionary(Dictionary *dict) for (unsigned long i = 0; i < dict->size; i++) Entry *curr = dict->buckets[i]; while (curr) Entry *temp = curr; curr = curr->next; free(temp->key); free(temp->value); free(temp);

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

Implement a function that allows you to loop through all key-value pairs currently in the table. If you'd like to improve this code, tell me: Should it handle dynamic resizing ? Both are too slow

// Delete a key printf("\nDeleting 'orange'...\n"); if (delete_key(dict, "orange")) printf("'orange' deleted successfully.\n"); else printf("'orange' not found.\n");

typedef struct Dict Node **table; // array of linked list heads int size; // capacity of the hash table int count; // number of key-value pairs stored Dict;

Using raw strings in C requires careful memory lifecycle management. The strdup() function creates a copy of the input string on the heap. This prevents bugs that happen if the caller passes a temporary or stack-allocated string that goes out of scope while the dictionary is still in use. Computational Complexity Analysis Average Case Worst Case Search Deletion