0

I am doing a project for inserting dictionary word into hash table. If collision occurred, hash the new word into same position to the next. I created an array of pointers:

typedef struct WordNode * WordNodeP;
typedef struct WordNode
{
    char word[WORDSIZE];
    struct WordNodeP *next;
}WordNodeT;
WordNodeP table[TABLESIZE];

and I hash each word in dictionary into the pointer of array by following function:

Hashfunction:

int hashFunction(char word[])                   //This function takes a string of word and break down each letter
{                                           //to integer value and use hash function. Then sum up all the values.
int i = 0;
int sum = 0;
while(word[i]!='\0'){
   if((int)word[i] < 97)      //If the ASC value of the letter is less than 97. It means it is a cap letter
       sum+= 3*(i+1)*(i+1)*(word[i]+32);
   else
        sum+= 3*(i+1)*(i+1)*word[i];
    i++;
}
if(sum > TABLESIZE)            //TABLESIZE = 12001;
    sum = sum % TABLESIZE;
return sum;
}

addWord:

void addWord(int val,char word[])             //This function adds words to specific index of array of LinkedList
{
    if(table[val]!=NULL){                       //If the position is occupied
    WordNodeT *temp = createNewNode(word);

    temp->next = table[val]->next;
    table[val]->next = temp;
}
    //printf("%s",table[val]->word);

    //printf("%s\n",table[val]->word);
else
    table[val] = createNewNode(word);                 //hash in the word to the table
}

CreateNewNode:

WordNodeP createNewNode(char word[])
{
WordNodeT* newNode = (WordNodeT*)malloc(sizeof(WordNodeT));

strcpy(newNode->word,word);
//printf("%s", newNode->word);
newNode->next = NULL;

return newNode;

}

The issue is in lookup function. No matter which word is passed in, it always returns 0(not found);

lookup:

int lookup(char * word)                                    //see if word exists in the dictionary
{
int val;
val = hashFunction(word);
printf("hash value is %d\n",val);
WordNodeT *temp = table[val];
while(temp!=NULL){           //all the way through
    if(strcmp(temp->word,word)== 0){
        return 1;
    }
    temp = temp->next;
}
return 0;

}

Note that I put

printf("hash value is %d\n",val) 

in the lookup for checking hashValue.

In the main, I code:

printf("%d\n",lookup("hello"));

For output, I got 5948 for hashValue and 0 for return value. Since I have 0 for return value,then I code for checking purpose:

    WordNodeT *temp = table[5948];
while (temp->word!=NULL){
    printf("%s",temp->word);
    temp = temp->next;
}

It prints out other words, one of them is "scallop", I calculated it's hashValue, the hashValue of "scallop": ((1^2*3*115)+(2^2*3*99)+(3^2*3*97)+(4^2*3*108)+(5^2*3*108)+(6^2*3*111)+(7^2*3*112)) % TABLESIZE(which is 12001) = 9885;

So can you guys help me with this?

Yizhi Hu
  • 57
  • 8
  • 1
    The answer is, "Don't typedef pointers". So remove the first line of code in the question, and the fix the rest of the code as needed. – user3386109 Feb 03 '18 at 21:51
  • 1
    The first thing I notice is `97`. Please don't use magic numbers. Even if you are sure of ASCII encoding it is more readable as `'a'`. Followed by `word[i]+32` which would be better as `tolower(word[i])`. Also consider the function `isupper` and family. – Weather Vane Feb 03 '18 at 21:51

0 Answers0