So far I've been unable to get the dictionary to load properly in the speller problem set. The goal is to insert every word from the dictionary into a hash table.
Is what I'm doing in the the line where I set p=n (last line of the else loop) a valid way to make two node pointers point to the same node? I have a feeling that the problem may lie around there
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
// Open dictionary file
FILE *file = fopen(dictionary,"r");
if (file == NULL)
{
return false;
}
// Create memory space to read new word
char bloc[LENGTH];
// Read words one at a time until end of file is reached
while (fscanf(file, "%s", bloc) != EOF)
{
// Create a new node and give as its value the new word
node *n = malloc(sizeof(node));
strcpy (n->word, bloc);
// Generate hash value given new word
unsigned int i = hash(n->word);
// Insert new word into hash table
if (index[i] == NULL)
{
index[i] = n;
}
else
{
node *p = malloc(sizeof(node));
p->next = index[i]->next;
while (p->next != NULL)
{
p = p->next;
}
p = n;
}
}
fclose(file);
return true;
}