I am implementing a data structure; a double linked list where items are in range. I want to find whether an item is present or not in O(1). For this, I would like to hash the nodes where the key will be the item & value will be the node.
In Java, there are in built functions to support this kind of feature.
EDIT: In short,i want hashMap kind of thing in C.
What should I do to implement it in C?
Create an array called “buckets” that contain the key and value, with an optional pointer to create a linked list.
When you access the hash table with a key, you process the key with a custom hash function which will return an integer. You then take the modulus of the result and that is the location of your array index or “bucket”. Then you check the unhashed key with the stored key, and if it matches, then you found the right place.
Otherwise, you’ve had a “collision” and must crawl through the linked list and compare keys until you match. (note some implementations use a binary tree instead of linked list for collisions).
Check out this fast hash table implementation:
http://attractivechaos.awardspace.com/khash.h.html