0

As far as I can tell the code I have seems correct, though when I try to print out what my table contains, I get repeats of the same value.

I can't tell if I am printing the data from my hashtable incorrectly or if I am inputting data to the hashtable incorrectly.

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"
void printHash();
// Returns true if word is in dictionary else false
int main(void)
{     load("dictionaries/small");
}

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

unsigned int hash(const char *word)
{
return tolower(word[0]) - 'a';
}
int count = 0;

bool load(const char *dictionary)
{
// TODO
// For each word in the dictionary, store it in a data structure
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
    printf("Could not load file.");
    return false;
}
printf("%s\n", dictionary);
char *word[40];
node *hashtable[26];
// node *head[4];

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

while(fscanf(file, "%s", *word) != EOF)
{

    node *new_node = calloc(1, sizeof(node));
    if (new_node == NULL)
    {
        //unload();
        free(hashtable);
        free(new_node);
        return false;
    }

    else

    {

        strcpy(new_node->word, *word);
        new_node->next =NULL;
        int bucket = hash(new_node->word);

    node *temp = hashtable[bucket];
        if (hashtable[bucket])
        {
        for (node *ptr = hashtable[bucket]; ptr != NULL; ptr = ptr->next)
        {
            if (!ptr->next)
            {
                ptr->next = new_node;
                count++;
                break;
            }
        }
        }
        // //first
        // if(hashtable[bucket] == NULL)
        // {
        //     //new_node->next =hashtable[value];
        //     hashtable[bucket] = new_node;

        //     count++;

        // }
        else
        {

            hashtable[bucket] = new_node;
            new_node->next = temp;
            count++;

        }
    }

  }
  printf("%i\n",count);
printHash(hashtable);
// Close file
fclose(file);

// Return bool
return true;
}

Code for the print function is here:

void printHash(node *hashtable[26])
{
node *ptr = calloc(1, sizeof(node));
for(int i=0; i < 26; i++)
{
    if(hashtable[i] != NULL)
    {
        ptr->next = hashtable[i];
         while(ptr->next != NULL)
        {
                printf("%s", hashtable[i]->word);
                printf("\n");
                ptr = ptr->next;

        }
    }
}

}

0

I guess scanf would work better when passed a pointer to memory it can write to. Your *word might point anywhere.

char *word[40];

declares an array of 40 pointers. Which 40 strings do you want to point to? You likely meant

char word[LENGTH + 1];

(don't forget to then change *word, which is equivalent to word[0], to just word)

Your hash table should be stored in a global variable, so that other functions can access it. Maybe there is one already in the provided code for hash table type implementations? Also, don't hard-code the size everywhere, but use a constant or a #define (guess that's also in the distribution code).

You can't free a variable not allocated on heap, like for example your hashtable variable.

Insert the new node at the start of the list, that's like two lines and much faster (though your code for that seems inefficient and hard to read, but correct).

The calloc of a bogus node in your print function makes little sense to me, and check the node itself and not its next pointer in the loop, so that you don't skip the last entry.

You must log in to answer this question.

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