I realize that many people have asked this question before but I didn't find my answer in any of them. Or maybe I'm just too stupid to see it. Anyways my code seems to work except that all the words are considered misspelled. I think it might be because of check function but I'm not sure how to fix it. please help me figure it out. Thanks!
// Implements a dictionary's functionality
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include "dictionary.h"
// Represents number of buckets in a hash table
#define N 26
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Represents a hash table
node *hashtable[N];
// Hashes word to a number between 0 and 25, inclusive, based on its first letter
unsigned int hash(const char *word)
{
return tolower(word[0]) - 'a';
}
//initialize words counter & head pointer
int count = 0;
node *head;
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// Initialize hash table
for (int i = 0; i < N; i++)
{
hashtable[i] = NULL;
}
// Open dictionary
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
unload();
return false;
}
// Buffer for a word
char word[LENGTH + 1];
// Insert words into hash table
while (fscanf(file, "%s", word) != EOF)
{
/// TODO ///
//hash the word
int k = hash(word);
//store address of node into head
head = hashtable[k];
//allocate space for a node
hashtable[k] = (node*)malloc(sizeof(node));
//verify that we have space to allocate for that node
if (hashtable[k] == NULL)
{
printf("not enough space in memory\n");
unload();
return false;
}
//copy word into node
strcpy(hashtable[k]->word, word);
if (head == NULL)
{
//make that node point to NULL
hashtable[k]->next = NULL;
//intcrement words counter
count++;
}
else
{
//link new node to the old head
hashtable[k]->next = head;
//make the new node head of the linked list
head = hashtable[k];
//intcrement words counter
count++;
}
}
// Close dictionary
fclose(file);
// Indicate success
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
/// TODO ///
return count;
}
// Returns true if word is in dictionary else false
bool check(const char *word)
{
/// TODO ///
int k = hash(word);
node *cur = hashtable[k];
//go through node and search for word
while (cur != NULL)
{
//compare word in text with word in dictionary
if (strcasecmp(hashtable[k]->word, word) == 0)
{
return true;
}
cur = cur->next;
}
return false;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
/// TODO ///
//initialize cur to head
node *cur = head;
//loop through nodes until hitting NULL
while (cur != NULL)
{
//store cur into temp
node *temp = cur;
//make cur point to next node
cur = cur->next;
//free node pointed to by temp
free(temp);
}
return true;
//return false;
}