C Program To Implement Dictionary Using Hashing It Out

Posted on by
C Program To Implement Dictionary Using Hashing It Out 8,3/10 1083votes

A quick hashtable implementation in c. Use Jenkins Hashing Algo. You could actually use this beast to implement a pseudo array that can have named.

C Program To Implement Dictionary Using Hashing It Out

One of the things which I miss while writing programs in C is a dictionary data structure. What's the most convenient way to implement one in C? I am not looking for performance, but ease of coding it from scratch. I don't want it to be generic either -- something like string->int will do. But I do want it to be able to store an arbitrary number of items.

This is intended more as an exercise. I know that there are 3rd party libraries available which one can use. But consider for a moment, that they don't exist. In such a situation what's the quickest way you can implement a dictionary satisfying the above requirements.

• Hash table will have 'n' number of buckets. • To insert a node into the hash table, we need to find the hash index for the given key. And it could be calculated using the hash function. Example: hashIndex = key% noOfBuckets • Move to the bucket corresponds to the above calculated hash index and insert the new node at the end of the list. • To delete a node from hash table, get the key from the user, calculate the hash index, move to the bucket corresponds to the calculated hash index, search the list in the current bucket to find and remove the node with the given key.

Finally, remove the node with given key, if it is present. Hash table with 5 buckets. 0, 1,2,3 and 4 are the hash indexes +-------+ 0 +-------+ 1 +-------+ 2 +-------+ 3 +-------+ 4 +-------+ Insert a node with key 33 into the hash table.

HashIndex = 33% 5(no of buckets) hashIndex = 3 Hash index is 3. So, insert the new node to the bucket with hash index 3. +-------+ 0 +-------+ ----------------- ------------------ 1 ---->21 data - ---->31 data - ----->X +-------+ ----------------- ----------------- 2 +-------+ ------------------ 3 --->33 data --->X +-------+ ------------------ 4 +-------+ Delete a node with key 31 from the hash table. HashIndex = 31% 5(no of buckets) = 1 Move to the bucket with above calculated hash index(1), search the list in the current bucket(bucket with index 1)to find the node with given key and delete it. +-------+ 0 +-------+ ----------------- 1 ---->21 data - ---->X +-------+ ----------------- 2 +-------+ ------------------ 3 --->33 data --->X +-------+ ------------------ 4 +-------+.

Snfu If You Swear Rarity on this page. Jp@jp-VirtualBox:$./a.out Enter the number of elements:3 1. Exit Enter your choice:1 Enter the key value:3 Name:Sally Age:23 1. Typing Master Software Ei. Exit Enter your choice:1 Enter the key value:33 Name:Harry Age:25 1. Exit Enter your choice:1 Enter the key value:7 Name:Nick Age:30 1.

Exit Enter your choice:1 Enter the key value:35 Name:Raj Age:28 1. Exit Enter your choice:4 Data at index 0 in Hash Table: VoterID Name Age -------------------------------- 33 Harry 25 3 Sally 23 Data at index 1 in Hash Table: VoterID Name Age -------------------------------- 7 Nick 30 Data at index 2 in Hash Table: VoterID Name Age -------------------------------- 35 Raj 28 1. Exit Enter your choice:2 Enter the key to perform deletion:33 Data deleted successfully from Hash Table 1. Exit Enter your choice:4 Data at index 0 in Hash Table: VoterID Name Age -------------------------------- 3 Sally 23 Data at index 1 in Hash Table: VoterID Name Age -------------------------------- 7 Nick 30 Data at index 2 in Hash Table: VoterID Name Age -------------------------------- 35 Raj 28 1. Exit Enter your choice:3 Enter the key to search:35 VoterID: 35 Name: Raj Age: 28 1.

Exit Enter your choice:5.