0

So I have code that I feel as though should be working, but I'm having trouble checking my work.

I wrote a function that I think loops through my table, but it only displays half of the data seemingly.

So I can't tell if my load() logic is broken, or my printHash() logic is broken.

Any help would be appreciated. Thanks!

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"
void printHash();

#define N 26
// Returns true if word is in dictionary else false

// Main function only so I can run load() without other files
int main(void)
{
    load("dictionaries/small");
}

// Defines the structure of a node
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Hash function to get index value of a letter.
unsigned int hash(const char *word)
{
    return tolower(word[0]) - 'a';
}

// Keep track of the number of words in the dictionary.
int count = 0;

// Create the hashtable
node *hashtable[N] = {};
// Loads dictionary and stores the words in an alphabetical hashtable.
bool load(const char *dictionary)
{

// Load file, if file is NULL throw error.
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
    printf("Could not load file.");
    return false;
}

// Prints name of dictionary that is being read
printf("%s\n", dictionary);

// Buffer for words
char word[LENGTH + 1];




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

// Scan through file one word at a time until end is reached.
while(fscanf(file, "%s", word) != EOF)
{
    // Allocate space for a new node
    node *new_node = calloc(1, sizeof(node));

    // If space not found, throw error.
    if (new_node == NULL)
    {
        //unload();
        free(hashtable);
        free(new_node);
        return false;
    }

    else

    {
        // Copy word from buffer onto node.
        strcpy(new_node->word, word);

        // Set index value for current word
        int bucket = hash(new_node->word);

       // node *temp = hashtable[bucket];

        // Checks if current index is null
        if(hashtable[bucket] == NULL)
        {
            hashtable[bucket] = new_node;
            new_node->next = NULL;
            count++;

        }
    else
        {

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

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

// Return bool
return true;
}

//Create a node to iterate through hashtable
// Loop through length of hashtable
//     Set node to ith element of hashtable
//     While node is not null
//         Print node value
//         Advance pointer to next item in linked list
void printHash()
{
node *ptr = calloc(1, sizeof(node));
for(int i=0; i < N; i++)
{
    if(hashtable[i] != NULL)
    {
        ptr = hashtable[i];
         while(ptr != NULL)
        {
                printf("%s", hashtable[i]->word);
                printf("\n");
                ptr = ptr->next;

        }
    }
}
}

To illustrate what I mean: This is my dictionary. And this is my output.

Thanks in advance!

0

Have you tried debug50, introduced in Week 2? There is a short here, debug50 starts around 12:11. Naturally, there is a learning curve, but it doesn't take long to master it. And it's really worth the effort to learn.

A suggestion: use a smaller test file. Based on the dictionary v the output, you should be able to duplicate that with a dictionary of 2 words (maybe 4 for a bigger sample). That would/should make using debug50 and finding the problem a snap!

You must log in to answer this question.

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