Skip to content

Commit

Permalink
Rewirte hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
chinhungtseng committed Mar 8, 2021
1 parent 9177d50 commit e0c1922
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions week5/pset5/speller/dictionary.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ bool check(const char *word)
// Hashes word to a number
unsigned int hash(const char *word)
{
int sum = 0;
const char *a = word;
int number = 0;

while (*a) {
sum += tolower(*a);
a++;
while (*word) {
number += tolower(*word++);
if (*word)
number += ((int) tolower(*word++)) << 8;
}

return sum % N;
return number % N;
}

// Loads dictionary into memory, returning true if successful, else false
Expand All @@ -72,10 +72,11 @@ bool load(const char *dictionary)
node *new_node = malloc(sizeof(node));

strcpy(new_node->word, word);
new_node->next = NULL;

if (*head)
new_node->next = *head;
else
new_node->next = NULL;

*head = new_node;

Expand Down

0 comments on commit e0c1922

Please sign in to comment.