0

Please assist me how to resolve such an error. see my code below:

// Scans dictionary word by word (populating hash table with nodes containing words found in dictionary) char word[LENGTH + 1]; while (fscanf(file, "%s", word) != EOF) { // Mallocs a node for each new word (i.e., creates node pointers) node *new_node = malloc(sizeof(node)); // Checks if malloc succeeded, returns false if not if (new_node == NULL) { unload(); return false; } // Copies word into node if malloc succeeds strcpy(new_node->word, word);

    // Initializes & calculates index of word for insertion into hashtable
    int h = hash_index(new_node->word);

    // Initializes head to point to hashtable index/bucket
    node *head = hashtable[h];

    // Inserts new nodes at beginning of lists
    if (head == NULL)
    {
        hashtable[h] = new_node;
        word_count++;
    }
    else
    {
        new_node->next = hashtable[h];
        hashtable[h] = new_node;
        word_count++;
    }
}
fclose(file);
return true;

}

// Returns true if word is in dictionary else false bool check(const char *word) { // Creates copy of word on which hash function can be performed int n = strlen(word); char word_copy[LENGTH + 1]; for (int i = 0; i < n; i++) { word_copy[i] = tolower(word[i]); } // Adds null terminator to end string word_copy[n] = '\0'; // Initializes index for hashed word int h = hash_index(word_copy); // Sets cursor to point to same address as hashtable index/bucket node *cursor = hashtable[h]; // Sets cursor to point to same location as head

// If the word exists, you should be able to find in dictionary data structure.
// Check for word by asking, which bucket would word be in? hashtable[hash(word)]
// While cursor does not point to NULL, search dictionary for word.
while (cursor != NULL)
{
    // If strcasecmp returns true, then word has been found
    if (strcasecmp(cursor->word, word_copy) == 0)
    {
        return true;
    }
    // Else word has not yet been found, advance cursor
    else
    {
        cursor = cursor->next;
    }
}
// Cursor has reached end of list and word has not been found in dictionary so it must be misspelled
return false;

}

// Returns number of words in dictionary if loaded else 0 if not yet loaded unsigned int size(void) { return word_count; }

// Unloads dictionary from memory, returning true if successful else false bool unload(void) { node *head = NULL; node *cursor = head; // freeing linked lists while (cursor != NULL) { node *temp = cursor; cursor = cursor->next; free(temp); } return true; }

1

Please try to correctly format your code so that I can debug it

| improve this answer | |

You must log in to answer this question.

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