Ello, I'm writing a program for a class that stores games in a hash table as an array of linked lists. Everything is working fine except the part where if the hash value is the same, it adds the node to the next spot in the linked list at the specified index in the array. What I mean is, it's not actually adding a new node to each linked list if the hash value is the same for two games and I can't seem to figure out why. Here is some relevant code:
Code:
struct game
{
char name[30];
struct game * next;
};
This is the *most* working my addNode has gotten, I know it's not correct.
Code:
void addNode(struct game ** array, char name[], int size)
{
int hashVal = getHashVal(name, size);
struct game * temp;
printf("%s %d\n", name, hashVal);
temp = (struct game *)malloc(sizeof(struct game));
strcpy(temp->name, name);
temp->next = NULL;
if(array[hashVal] == NULL)
{
array[hashVal] = temp;
}
else
{
while(array[hashVal]->next != NULL)
{
array[hashVal] = array[hashVal]->next;
}
if(array[hashVal]->next == NULL)
{
array[hashVal]->next = temp;
}
}
}
Here is the function that figures out how many games are at each location in the hash table:
Code:
int getNumGames(char word[], struct game ** array, int size)
{
int hashVal;
int cnt = 0;
hashVal = getHashVal(word, size);
if(array[hashVal] == NULL)
{
return 0;
}
while(array[hashVal]->next != NULL)
{
cnt++;
array[hashVal] = array[hashVal]->next;
}
return cnt;
}
It is saying that there is 1 game in location 0 (supposed to be three), and 0 at location 4 (supposed to be 1). Also here is the input file:
Code:
APPLES_TO_APPLES
BOGGLE
CANDYLAND
CHESS
GO_FISH
MONOPOLY
POKER
SCRABBLE
TWISTER
UNO
WAR