0

Whenever I load words from the dictionary and check the number of words in the dictionary, it seems to load one more than there actually is. For example, if I load the SMALL dictionary, number of words are 3 and for LARGE dictionary it is 143092. I have been stuck with this for the last few days and any help would be really appreciated. Thank you

bool load(const char* dictionary)
{
// TODO
FILE* dict = fopen(dictionary,"r");

if(dict == NULL)
{
    printf("CANNOT FIND DICTIONARY\n");
    return false;
}

for(int i = 0; i < 26; i++)
{
    hashtable[i] = NULL;
}



for(int i = 0; !feof(dict); i++)
{
    node* new_node = malloc(sizeof(node));
    new_node->next = NULL;

    fscanf(dict,"%s",new_node->word);

    int key = hash_function(new_node->word);

    if(hashtable[key] == NULL)
    {
        new_node->next = NULL;
        hashtable[key] = new_node;
    }

    else
    {
        node* prev = NULL;
        node* cursor = hashtable[key];

        while(cursor != NULL && new_node->word > cursor->word)
        {
            prev = cursor;
            cursor = cursor->next;
        }

        new_node->next = cursor;
        prev->next = new_node;

    }


}
return true; 

}
1
  • How is prev defined and what is it supposed to be doing? Cursor looks like it's navigating the hash table, is that right?
    – ronga
    Nov 21 '15 at 1:43
1

This line:

for(int i = 0; !feof(dict); i++)

The feof indicator will not be set until AFTER fscanf finds the end of file. So you will have to read past the end of file, store it (i have no idea how you're hashing the words, so I don't know if the key will be a valid entrance, I don't know how you're counting the words or where, which would be a relevant piece of information here). But feof will not be set until after the "word" has been read.

BTW, what is this supposed to do?

new_node->word > cursor->word

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .