To Implement Dictionary Using Hashing Algorithms — C Program
For this implementation, we will use strings as keys. Here are three widely used hash algorithms for strings: Simple, fast, and provides good distribution.
char* search(Dictionary *dict, const char *key) unsigned long hash = dict->hash_func(key); unsigned long index = hash % dict->size; Entry *curr = dict->buckets[index]; while (curr) if (strcmp(curr->key, key) == 0) return curr->value; curr = curr->next; c program to implement dictionary using hashing algorithms
dict->size = INITIAL_SIZE; dict->count = 0; dict->hash_func = hash_djb2; For this implementation, we will use strings as keys
unsigned long hash_crc(const char *str) unsigned long hash = 0xFFFFFFFF; int c; while ((c = *str++)) hash = (hash >> 8) ^ crc32_table[(hash ^ c) & 0xFF]; return hash ^ 0xFFFFFFFF; For this implementation