Thread: Issue with Hash Table

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    81

    Issue with Hash Table

    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

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    while(array[hashVal]->next != NULL)
    {
        array[hashVal] = array[hashVal]->next;
    }
    When you do this in your addNode and getNumGames functions, you are actually changing the array permanently, and you are only keeping at most the last element in any chain of hashes. Try using a temp pointer instead, something like:
    Code:
    struct game *p;
    p = array[hashVal];
    while (p->next != NULL)
    {
        p = p->next;
    }

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    Shoot, forgot about that. That and a little typo and I fixed it! Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hash table
    By Aisthesis in forum C++ Programming
    Replies: 10
    Last Post: 09-22-2010, 08:58 AM
  2. Hash Table
    By siavoshkc in forum C++ Programming
    Replies: 4
    Last Post: 08-29-2006, 04:29 PM
  3. hash table!
    By spank in forum C Programming
    Replies: 4
    Last Post: 04-10-2006, 11:09 AM
  4. What is a Hash Table?
    By rip1968 in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2002, 08:57 PM
  5. Hash table
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-14-2002, 07:06 PM