BRIAN YU: In the check function, your job is to take a word
and check to see if the word is in the dictionary or not.
The check function will take a char star or a string
representing the word as its input, and the output
will be a Boolean variable, true or false.
So what does your check function need to do?
Well, your function should return true if the word is in your dictionary.
And it should return false otherwise.
And importantly, your check function should be case insensitive.
It shouldn't matter if some of the characters in the word
are uppercase or lowercase.
Your spell checker should still be able to detect whether or not the word is
in the dictionary regardless of case.
So what are you going to need to do?
Well, the first step is going to be to take the word
and hash the word using the hash function that you implemented earlier
in order to obtain a hash value.
Then, you'll access the linked list at that hash value
at that particular index in your hash table, which, again, is just
an array of those individual linked lists.
Once you've access that individual linked list,
you know that if the word is in the dictionary,
it's going to be in that list.
And if the word is not in that list, then it's not in the dictionary.
So you can traverse that linked list, looking at one node
at a time looking for the word in question
and using the strcasecmp function, which is similar to strcmp, which
compares to strings, but strcasecmp will compare two strings case insensitively.
So it won't matter whether the word is uppercase or lowercase for example
in order to find the word.
If you're able to successfully find the word, your function will return true.
And otherwise, your function will return false.
How do you traverse a linked list though?
Well, let's take a look at an example.
Here, let's imagine that head is a variable that
is a pointer to the first element in the linked list, in this case, the word
bat, which is inside of a node that points
to a node that has the word book, which points to banana, for example.
How might I traverse that linked list?
Well, you could imagine that I could set up a variable called
cursor, for example, which initially is a pointer to the first element
in the linked list.
And I might check, is that the word that I'm looking for?
And if it's not the word I'm looking for, then what I'd like to do
is take the cursor and move it to the next node in the linked list.
Well, where's the next node stored?
It's stored inside of the next field inside of the node
that I'm currently looking at.
So if I were to use syntax like cursor equals cursor arrow next,
that would mean take cursor and set it equal to whatever cursor's next pointer
is currently pointing to.
So that would move cursor to the next node in the linked list.
And I could check this node to see if this is the correct word, again, using
the strcasecmp function to compare two strings to see case insensitively
whether they contain the same characters or not.
If that's not right, then I might repeat the process setting
cursor equal to cursor arrow next to move the cursor to the next node.
If I ever find the word in question, I'll go ahead and return true.
But when do I stop?
You might imagine that this is happening in some sort of loop.
And eventually, I'll reach the end of the linked list, where, at some point,
I'll set cursor equal to cursor arrow next.
And at the end of the linked list, what we'll have is null.
So if we move forward cursor equals cursor arrow next
and eventually cursor becomes null, that means
we've reached the end of the linked list and the word
wasn't found, which means the word isn't in our dictionary
and our function should at this point return false.
So as a brief recap, how are we going to traverse this linked list?
You'll start with some cursor set to the first node inside of your linked list.
And then you'll keep moving the cursor, setting
it equal to cursor arrow next over and over again checking each word
as you go until you get to null.
If you find the word along the way, then that
means the word is in the dictionary and you can return true from this function.
And otherwise, if you reach the end of the list,
then the word is not in the dictionary and you can return false.
As a result, your check function will be able to take any word
and determine whether or not it's inside of your dictionary.