0

When I compile my code it gives correct results, but doesn't pass check 50. Could please anyone help me to find a mistake, thank you.

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


#include "dictionary.h"
int word_count = 0;

// 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 list = 27;
node*hashtable[list] = {NULL};

// Hash table


// Returns true if word is in dictionary else false
    bool check(const char *word)
    {
    unsigned int key = hash(word);
    node *tmp = hashtable[key];
    while( tmp != NULL) //loop until the end of the list
    {
        if(strcasecmp(word, tmp->word) == 0) //compare if the word found
        {
            return true;
        }
        else
        {
            tmp = tmp->next; // if not found go to next node
        }

    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
  //Adapted by Neel Mehta from
  //stackoverflow.com/questions/2571683/djb2-hash-function.
  unsigned long hash = 5381;

     for (const char* ptr = word; *ptr != '\0'; ptr++)
     {
         hash = ((hash << 5) + hash) + tolower(*ptr);
     }

    return hash % 26;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    char word [LENGTH + 1] = {'\0'};
    //open file
    FILE *f = fopen(dictionary, "r");
    if (f == NULL) // check if file was opened succesefully
    {
        return false;
    }
    while (fscanf(f, "%s", word) != EOF) //
    {


        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return false;
        }
        strcpy(n ->word,word);// copy words to node n
        int i = hash(word);
        if( hashtable[i] == NULL)//setting list pointer to a new node
        {
            hashtable[i] = n;
            n->next = NULL;
        }
        else
        {
            n->next = hashtable[i];
            hashtable[i] = n;

        }
        word_count++;
    }
    fclose(f);
    return true;
}

// 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)
{
    for(int i = 0; i < 26; i++) //itterate through hashtable
    {
        node *n = hashtable[i];
        node*curse = n->next;
            while ( n != NULL)
            {
               curse = n;
               n = curse->next;
               free(curse);
            }
    }

    return true;
}```
1

In your unload function, you're forgetting that 'hashtable[i]->next' could or could not be inicialized, so when you try to get the pointer here: node*curse = n->next;, sometimes you'll get a memory access error.

1
  • thank you so much, I fixed it and it works May 21 '20 at 17:02

You must log in to answer this question.

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