Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
- 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. You can make WordNode a class if you want to.
- 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 (called myHashFunction). This hash function must call the djb2 function (from the hash tables lecture; 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).
- To be absolutely explicit, you should have two separate functions for this. Modularity is always your friend.
- I will be testing using lowercase only so you don't have to worry about case-sensitivity.
- 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() (or cout) to display "Success!". If you don't find the word, use printf() (or cout) to display "Not there!". This will continue in a loop until the user enters "." as the only thing on the input line.
- Do not have any other input or output.
- You should comment as normal.
Here is my djb hash function
unsigned long hash(unsigned char *str){
unsigned long hash=5381;
int c=0;
while((c=*str++)!='\0')
{
hash=((hash<<5)+hash)+c
}
return hash;
}
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