0

this is my first ask. I ever looked up all the answers and managed well alone but i am really stuck now.

So, i am doing the load and check functions of dictionary. I did a test program with all what i would use in the dictionary itself and when it worked i adjusted it to dictionary, but it is not working.

I tested all the ways i can and now i think the problem is in the load function. I really think my linked list is not working well but i really dont know why. The loop what look for each word, i already did it for all the forms i thought would work (for loops, errors check, etc) and nothing.

Here is my code. Im sure the issue is areally stupid thing but i really cant see.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "dictionary.h"
#include <string.h>

#define SIZE 26
#define LENGTH 45

typedef struct node
{
    char word[LENGTH + 1];
    struct node* next;
}
node;

int hash_f(char* key)
{
    int n = key[0]-'a';
    return n % SIZE;
}

// Define the hashtable
node* hashtable[SIZE];

int count_words = 0;

bool check(const char* word)
{
// copy the word to check
int n = strlen(word);
char n_word[LENGTH + 1];

//descapitallize all word
for (int b = 0; b < n; b++)
{
    n_word[b] = tolower(word[b]);
}
n_word[strlen(word) + 1] = '\0';

// Check
int num = hash_f(n_word);
node* src = hashtable[num];

// This tell me that src is ever NULL. fix it.
if(src != NULL)
{
    while (src != NULL)
    {
        if(strcmp(src->word, n_word) == 0)
        {
            return true;
        }
        src = src->next;
    }
}
return false; 
}

   /**
   * Loads dictionary into memory.  Returns true if successful else false.
   */
bool load(const char* dictionary)
{
for (int a = 0; a < SIZE; a++)
{   
    hashtable[a] = malloc(sizeof(node));
   // hashtable[a] = NULL;
}

FILE* file = fopen(dictionary, "r");
if (file == NULL)
{
    printf("error, retry\n");
    return false;
}

//node* head = NULL;
char c_word[LENGTH];


// read each character in dictionary

int index = 0;

for(int c = fgetc(file); c != EOF; c = fgetc(file))
{
    if (isalpha(c) || (c == '\'' && index > 0))
    {
        // append character to word
        c_word[index] = c;
        index++;

     if (index >= LENGTH)
        {
            // consume remainder of alphabetical string
            while ((c = fgetc(file)) != EOF && isalpha(c));

            // prepare for new word
            index = 0;
        }
    }
    else if (index > 0)
    {
        // terminate current word
        c_word[index] = '\0';

        node* new = malloc(sizeof(node));
        strcpy(new->word, c_word);
        int num = hash_f(new->word);
            if (hashtable[num] == NULL)
            {
            hashtable[num] = new;
            }
        else
        {
        new -> next = hashtable[num] -> next;
        hashtable[num] = new;
        }
        index = 0;
        count_words++;
    }
}

fclose(file);
return true;
}

One week working in this and i am very depressed. I dont know what i am doing of my life.

1 Answer 1

1

You have three problems - two in load and one in check.

In load, your first problem is actually two problems - you malloc the space before you need it and you forget to initialize the malloc'd memory, so it contains garbage data. This is a problem because your code depends on finding NULL for unassigned nodes - you actually had this right in the line that's commented out.

Second, the code that inserts a new node at the front of one of the hashtable's linked lists is broken. Look at this line:

        new -> next = hashtable[num] -> next;

You have no idea whether hashtable[num]->next even contains anything, and you end up throwing away hashtable[num] by doing this.

Finally, check() will never find anything. The good news is that it's a simple fix, but I'm only going to give you a clue. Insert this line in your check code following node* src = ... and it should be obvious:

        printf("Word=%s, n_word=%s.\n", word, n_word );

I can't take away all of the debugging fun! ;-)

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

1
  • This was absolutelly right! I cant believe. Thanks so much, now it is working perfectly. Commented Oct 28, 2015 at 15:57

You must log in to answer this question.

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