0

I've tried fixing this problem for a while now, but I can't seem to figure it out. There are two main issues I am having:

Firstly whenever I try running my code "./speller texts/lalaland.txt", it tells me that all of the words in the text are misspelled. It is quite frustrating, and I feel that my issue may extend beyond the function check, but I am unsure. I've tried so many different variations of the same function, but nothing seems to work.

Secondly, I'm having issues with memory leakage. This is an issue that exists, but I have not yet tried troubleshooting as much as the former. I'm more concerned with addressing the former problem first.

Any/all help addressing these issues is appreciated!

My code can be found below:


#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>

#include "dictionary.h"

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Number of buckets in hash table
const unsigned int N = 26; // chose 26 for simplicity 

// Hash table
node *table[N];

// Returns true if word is in dictionary else false
bool check(const char *word)
{

    int hashed = hash(word);
       for(node *cursor = table[hashed]; cursor != NULL; cursor = cursor->next) {
           if(strcasecmp(cursor->word, word) == 0) {
               return true;
           }
    }
       return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
        unsigned int value = tolower(word[0]) - 'a';
        return value;
}

int count = 0;

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    FILE* file = fopen(dictionary, "r"); 
    if (file == NULL) {
        return false;
    }
    
    for (int i = 0; i < N; i++) {
        table[i] = NULL;
    }
    
    char word[LENGTH + 1];
    
    while(fscanf(file, "%s", word) != EOF) {
        node *n = malloc(sizeof(node));
        if (n == NULL) {
            return false;
        }
        strcpy(n->word, word);
        unsigned int index = hash(n->word);
        count++;
        
        node *head = table[index];
        
        if (head == NULL) {
            head = n;
            n->next = NULL;
        }
        
        else {
            n->next = head->next;
            head->next = n;
            return true;
        }
        
        }
        fclose(file);
        return true;
}

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

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    for (int i = 0; i < N; i++) {
        node *head = table[i];
        node *curr = head;
        while(curr != NULL) {
            node *temp = curr;
            curr = curr->next;
            free(temp);
        }
        return true;
        
    }
    return false;
}
0

You're instincts are good. The problems are in load, not check.

There are two distinct problems, both causing data not to be stored in the tree.

First, the code does this:

 node *head = table[index];

This will copy NULL into head. The remaining code processes the current word, but ultimately is storing the address of the new node in head, not in table[index]. So, nothing in table[] ever gets updated, effectively, nothing is stored in the dictionary. This is also causing a lot of the memory leak issues.

The most efficient thing to do here is to just remove head from the code and use table[index] directly.

Next, is the code that handles adding words to the tree where table[index] is not null terminates processing of the dictionary.

    else {
        n->next = head->next;
        head->next = n;
        return true;
    }

The return statement ends processing of the dictionary instantly. It needs to run until the dictionary is fully processed.

There are still a couple of other issues, but I'll give you a chance to work on them. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Thank you so much!!! :) – Kevin Rodriguez Jul 19 '20 at 1:01
  • I fixed the memory issues by changing the end of my unload function. I got rid of the return true right after at the end of the while loop + changed what was previously return false to return true. – Kevin Rodriguez Jul 19 '20 at 1:14

You must log in to answer this question.

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