Question
unsigned long hash(unsigned char *str) { unsigned long hash = 5381; int c = 0; while ((c = *str++) != 0) hash = ((hash <
unsigned long hash(unsigned char *str) { unsigned long hash = 5381; int c = 0; while ((c = *str++) != \0) hash = ((hash << 5) + hash) + c; return hash; }
write a c program that stores words in a hash table and then searches for the words.
You will keep track of words that are 20 or fewer characters long. The nodes that you will create as structs will contain an array of char of length 21 and a pointer to the next node. Call this struct WordNode.
In main, declare an array variable to contain your hash table. Let the array buckets contain pointers to WordNodes. The size of the array is 11.
Create a loop that gets one word at a time from the user until they enter "." as the only thing on the input line.
Each time you get a word, pass the word as a parameter to a hash function. This hash function must call the djb2 function (from the top of the page and change the parameter to work with chars rather than unsigned chars) and then use the Division Method to return a value between 0 and 10 (inclusive) as a hash value. Use this returned hash value to determine which bucket should contain the word. Then add the word to the bucket as described in lecture. Make sure that your linked lists are sorted linked lists, sorted in ascending order (e.g. "alpha" would occur before "theta"). They do not have to have a tail or prev pointers but they can if you want (the tail pointer would now have to be in the bucket along with the head pointer).
Once the user is done entering words, set up a separate loop to get input for words to search for. Then search for the words in the hash table by determining the appropriate bucket and searching the linked list found there (if there is one). Every time you do a comparison in your search, use printf() (or cout) to display the word you are comparing your search word against. When you find the word, use printf() to display "Success!". If you don't find the word, use printf() to display "Not there!". This will continue in a loop until the user enters "." as the only thing on the input line.
You should comment as normal.
Call your primary source file mini3.c . Put your linked list code into linkedList.c Put your djb2 and hash functions into djb2.c . Put any other code into your already-existing mini3.c. You can create any .h files you need in order to support this but do not create any other .c. Call your project mini3.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started