I have reached an impasse with regards to this exercise. I believe that I have written the load function and it works. I also believe that the hash function works I think the size function returns a correct value
my check function is here
// Returns true if word is in dictionary else false
bool check(const char *word)
{
// TODO
//first hash word to get hash value
bool comparison;
unsigned int hashValue = hash(word);
//printf("The hash value is %d\n", hashValue);
int compareResult = 0;
node *theWord = table[hashValue];
printf("table[hashValue] : %c\n", (char)theWord->word);'''
while (theWord != NULL)
{
compareResult = strcasecmp(theWord->word, word);
if (compareResult == 0)
{
comparison = true;
}
else
{
comparison = false;
}
theWord = theWord->next;
}
return comparison;}
the load function
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// TODO
//use fopen and check if return value of fopen is NULL, ie check that the file is there and has contents , if contains data return true, otherwise false
FILE *theDictionary ;
theDictionary = fopen(dictionary, "r");
if (&fopen == NULL)
{
printf("Dictionary File not found");
return false;
}
else
{
return true;
}
//fscanf(file, "%s", word) file is from fopen, the file pointer, word place to read file into
//fscanf will return EOF when the end of the file is reached
//repeat until reach EOF
//malloc a new node, check if NULL, copy word into node using strcopy
//declare a char array to store each word
char word[LENGTH];
//an int to hold the index of the hash table
int index = 0;
//whiule there are lines in the file
while (fscanf(theDictionary, "%s", word) != EOF)
{
//allocate memory space of size LENGTH+1 and assign address to pointer
node *pointer = malloc(sizeof(node));
//check sufficient memory allocated
if (pointer == NULL)
{
return false;
}
strcpy(pointer->word, word);
//use hash function
// takes a string and returns an index
//insert into hash table, use correct order
// insert new node an point to next node, then point previous node to new node
//allocate the index to the node
index = hash(pointer->word);
//where to place word in hash table
//check to see if this is the first node
if (table[index] == NULL)
{
table[index] = pointer;
}
//if this is not the first node
else
{
pointer->next = table[index];
table[index] = pointer;
}
size(); // increments the total number of words in the file
free(pointer); // free the memory the pointer is using
}
```
the hash function
// Hashes word to a number
unsigned int hash(const char *word)
{
// TODO
int hash_key = tolower(word[0] - 'a') % 26;
return hash_key;
}
advice on fixing the load and check functions much appreciated, also advice on unload as well. I am finding it a little confusing, and have gone over the lectures multiple times and the C text I have "A Book on C Al Keeley & Ira Pohl Benjamin/Cummings California 1989"