//HASH FUNCTION. hashtable size is 65536
int hash_function(char* needs_hashing)
{
unsigned int hash = 0;
for (int i=0, n=strlen(needs_hashing); i<n; i++)
{
hash = (hash << 2) ^ tolower(needs_hashing[i]);
}
return hash % SIZE;
}
//CODE FOR CHECK
bool check(const char *word)
{
//copy the word from the text into a char array
char word_in_text[LENGTH+1];
strcpy(word_in_text, word);
//create a pointer for the purposes of traversing the linked list.
node* cursor = hashtable[hash_function(word_in_text)];
//while the pointer isn't null compare the word with the value of the pointer
while (cursor != NULL)
{
//compare word with whatever word cursor is pointing to
if (strcasecmp(cursor -> word, word_in_text) == 0)
{
return true;
}
cursor = cursor -> next;
}
return false;
}
//CODE FOR LOAD
bool load(const char *dictionary)
{
//open the dictionary
FILE* inptr = fopen(dictionary, "r");
if (inptr == NULL)
{
unload();
return false;
}
//loop to read in strings from the dictionary file until its end
while (fscanf(inptr, "%s", dword) != EOF)
{
//get enough space for the node, check to make sure malloc doesn't point to nothing, then fill in the node
node* new_node = malloc(sizeof(node));
if (new_node == NULL)
{
unload();
return false;
}
strcpy(new_node -> word, dword);
new_node -> next = NULL;
counter++;
//find out where to put the word in the hashtable
int hash = hash_function(new_node->word);
//put the newly created node in the hashtable
//if there's nothing at that index make the node the first item
if (hashtable[hash]== NULL)
{
hashtable[hash]= new_node;
}
else //make a linked list
{
new_node -> next = hashtable[hash];
hashtable[hash] -> next = new_node;
}
}
fclose(inptr);
return true;
}
//CODE FOR UNLOAD
bool unload(void)
{
for (int i = 0; i < SIZE; i++)
{
node* traverse_ptr = hashtable[i];
while (traverse_ptr != NULL)
{
node* delete_me = traverse_ptr;
traverse_ptr = traverse_ptr -> next;
free(delete_me);
}
}
return true;
}
EDIT: code removed to comply with academic honesty