0

Ok, I got most of load working. I figured out the hash function, but now I am a little stuck. If you look at "HELP", you will see that I do not know the code syntax to insert my word into the appropriate hash array. All the examples I find online only deal with 1 linked list, not 50 as I will end up (potentially) having.

#define SIZE 50
typedef struct node
{

char word[LENGTH + 1]
struct node* next 

} node;

// Getting my head node
node* head = NULL;

bool load(const char* dictionary)
{

// 26 in alphebet array start like this: A(a-l(12))/A(m-z)(14). No word starts with /'.
// Uneven since beginning of alphebet more common.
node* hashtable[SIZE]

int counter = 0;
while (!feof(dictionary))
{

// I get memory for a new_node(which contains a dictionary word and pointer to next node)
// The fscanf actually gets the word
node* new_node = malloc(sizeof(node));
fscanf(dictionary, "%s", new_node->word);
counter++;

// I get the first char of the word in the node, and set that to key
char* key = new_node->word

//I throw it through my hash function, returning a value from 0 - SIZE
int hash = hash_func(key)

// HELP: Insert my word into hashtable array (by doing hashtable[hash]).

}


return false;
}

// Here I put the key through the hash function (key is word in the dictionary)
// I split hashtable into 52 groups. If the second word of the key is /' or A-L, then it gets the value 0-25 (sorted by 1st letter). 
// If the second word is M-Z, then I add 26 to it (so A(M-Z) words are in hash grp 26).
int hash_func (char* key)
{
int output = toupper(key[0]) - 'A';
    if (toupper(key[1] >= 'M' && <= 'Z'))
    {
        output += 26;
    }
return output % SIZE;

}

2

I will give you the pseudocode and let you convert it to actual code yourself.

if hashtable[hash] is NULL
    make hashtable[hash] point to new_node
else
    create a temporary_node
    make temporary_node point to where hashtable[hash] is pointing
    while temporary_node not NULL
        make temporary_node point to where temporary_node->next is pointing
    make temporary_node->next point to new_node

That should get you going. If you need more specific directions leave a comment.


If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

  • 1
    It also looks like another potential problem is waiting. You declare your hashtable array inside load. You should consider making it a global. – Cliff B Sep 15 '15 at 21:08
  • Really good point. – ChrisG Sep 15 '15 at 21:38
  • 1
    Thanks, with this help, I believe I figured it out. – Fijj Sep 16 '15 at 16:55

You must log in to answer this question.

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