0

Im trying to create a test function to print the contents of my hash array (after Load()) to make sure i've loaded everything correctly. But I think im getting stuck on the parameters needing to be passed to this array.

Load function:

bool load(const char *dictionary)
{
    FILE *file;
    file = fopen (dictionary, "r");
    char word[LENGTH];
    int index;

    for(int i = 0;i < HASHTABLESIZE; i++)
    {
        hashtable[i] = NULL;
    }

    while(fscanf(file, "%s", word) != EOF)
    {
        node *new_node = malloc(sizeof(node));
        if(new_node == NULL)
        {
            unload();
            return false;
        }
        int j=0;
        while(word[j])
        {
            word[j] = tolower(word[j]);
            j++;
        }
        strcpy(new_node->word, word);   
        index = hashValue(word);
        new_node->next = hashtable[index];
        hashtable[index] = new_node;
    }

printHash()
return true;
}

Struct and Array Definitions:

//Node Struct Definition
typedef struct node
{
    char word[LENGTH+1]; //+1 accounts for /0 (Null terminator)
    struct node *next;
}node;

node *hashtable[HASHTABLESIZE]; //Array of pointers that point to a node (struct) type

What I have so far:

void printHash(node *list, node *hashtable[HASHTABLESIZE])
{
    node *ptr = list;
    for(int i=0; i < HASHTABLESIZE; i++)
    {
        if(hashtable[i] == NULL)
        {
            i++;
        }
        else
        {
            while(ptr->next != NULL)
            {
                for(int j = 0; j < (LENGTH+1); j++)
                {
                    printf("%s", hashtable[j]->word);
                    printf("\n");
                    ptr = ptr->next;
                }
            }
        }
    }
}

So...im torn because I think I understand partially (maybe im wrong) on how to iterate through the hash table. Because i'd want to check each Index in the array of pointers (and if their NULL then nothings there so skip), and then go INTO the linked list it's pointing to.

But i've really struggled when it comes to pointers as parameters. I dunno why. For some reason pointers themselves make sense, but passing them is still a bit confusing. For instance in this case I dunno if im passing the right thing. Since well.....the linked list exists in memory, but it's not like I can pass a linked list into the parameter.....so i'd need to maybe just pass the array of pointers....and then make a new node pointer to point to each member of the linked list that the array points to? Maybe im way off.

I would say....maybe I should pass in new_node from Load() but that doesn't seem like it would make sense because it's pointing to something completely different.

Im just a bit stuck here and looking for guidance. Thanks!

1

A function to print the contents of a hash table is relatively quite simple, and I think you've overthought it a little. In pseudo-code, it would look like this:

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

You're most of the way there, but there are some elements that need looking at. Your nested for loop doesn't really make sense, and you need to make sure you don't create the possibility to advance i twice in one iteration. You also don't need to pass in any variables, as the hashtable array is already declared as a global variable, and I don't know what list is for!

2
  • There is also no need for me to pass either the array or pointer i'd guess either. Since on a normal linked list print function you are usually passing the HEAD pointer. But in this case the array is the head pointer which is global correct? – msmith1114 Feb 20 '17 at 15:16
  • 1
    Yes, in effect a hash table is just an array of head pointers so no need to pass any variables. – Steve Bunting Feb 20 '17 at 22:28

You must log in to answer this question.

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