Algorithms — C Program To Implement Dictionary Using Hashing
return hash % table_size;
// Define the structure for a dictionary typedef struct Dictionary int size; DictionaryEntry** table; Dictionary;
The delete_key function safely tracks the current node ( temp ) and the preceding node ( prev ). When it finds a matching key, it alters the pointers to stitch the list around the removed item. This preserves the links to any subsequent items in the chain, eliminating memory leaks. 7. Performance and Optimization Considerations
Are you interested in an alternative collision strategy like ? Share public link
The hash function converts a string key into an integer index. A common choice is the (multiplier 31), which minimizes collisions for strings. c program to implement dictionary using hashing algorithms
// Key doesn't exist: create new node and insert at head of linked list KeyValuePair *new_pair = create_pair(key, value); if (!new_pair) return;
Further reading: "The Art of Computer Programming, Vol 3" by Donald Knuth (Sorting and Searching)
(like linear probing or double hashing instead of linked lists)
int main() Dictionary myDict = 0 ; // Initialize buckets to NULL insert(&myDict, "Apple" , "A sweet red fruit" ); insert(&myDict, "C" , "A powerful programming language" ); char *result = search(&myDict, "Apple" ); if (result) printf( "Apple: %s\n" , result); return 0 ; Use code with caution. Copied to clipboard Quick Way to Implement Dictionary in C - Stack Overflow return hash % table_size; // Define the structure
int delete_key(HashTable *dict, const char *key) unsigned long index = hash(key, dict->size); Entry *curr = dict->buckets[index]; Entry *prev = NULL;
: A strategy (like Linear Probing) to manage cases where two different keys generate the same index. The C Implementation
#include #include #include #define TABLE_SIZE 10 // 1. Define the entry structure (Key-Value Pair) typedef struct Node char* key; char* value; struct Node* next; // For handling collisions via chaining Node; // 2. Define the dictionary (Hash Table) structure typedef struct Node* buckets[TABLE_SIZE]; Dictionary; // 3. Simple Hashing Algorithm (djb2) unsigned int hash(const char* key) unsigned int hash_val = 5381; int c; while ((c = *key++)) hash_val = ((hash_val << 5) + hash_val) + c; // hash * 33 + c return hash_val % TABLE_SIZE; // 4. Dictionary Operations Dictionary* dict_create() Dictionary* dict = malloc(sizeof(Dictionary)); for (int i = 0; i < TABLE_SIZE; i++) dict->buckets[i] = NULL; return dict; void dict_insert(Dictionary* dict, const char* key, const char* value) unsigned int index = hash(key); Node* new_node = malloc(sizeof(Node)); new_node->key = strdup(key); new_node->value = strdup(value); new_node->next = dict->buckets[index]; // Insert at head of chain dict->buckets[index] = new_node; char* dict_get(Dictionary* dict, const char* key) unsigned int index = hash(key); Node* temp = dict->buckets[index]; while (temp) if (strcmp(temp->key, key) == 0) return temp->value; temp = temp->next; return "Not Found"; int main() Dictionary* my_dict = dict_create(); dict_insert(my_dict, "C", "A powerful programming language"); dict_insert(my_dict, "Hash", "A function that maps data to a fixed size"); printf("Search 'C': %s\n", dict_get(my_dict, "C")); printf("Search 'Python': %s\n", dict_get(my_dict, "Python")); return 0; Use code with caution. Copied to clipboard Helpful Features for Your Dictionary
typedef struct HashTable Node** buckets; int size; HashTable; A common choice is the (multiplier 31), which
For this article, we will implement due to its simplicity and robustness.
A raw string cannot serve directly as an array index. The hash_function processes each character of the key string.
printf("\n");