Hashing, can't insert to hash table

198 views Asked by At
struct googlePlayApp{
    string name;
    string category;
    double rating;
    int reviews;
    googlePlayApp *next;
};
  void appInsert(googlePlayApp &newApp, int &cmps) {
    int slot = hash1(newApp.name, HASH_SIZE);
    int cmps1 = 1;
    googlePlayApp *tmp = appHash[slot];

    if (tmp == 0)
        appHash[slot] = &newApp;
    else
    {
        while(tmp->next != 0)
        {
            tmp = tmp->next;
            cmps1++;
        }
        tmp->next = &newApp;
    }
    cmps += cmps1;
}
    while (getline(inFile, inputLine)) {
        googlePlayApp newApp;
        readSingleApp(inputLine, newApp);
        appInsert(newApp, cmps);
        linesRead++;
    }

My program stops on the 65th iteration of the while loop.... 64th for the appInsert call... Why can't I get this to work? This is a program where it reads a data file and stores it in a hash table and collision dealt with open addressing....

updated question

bool appFind(const string &name, googlePlayApp &foundApp, int &cmps) {
    // Implement this function
    int slot = hash1(name);
    int cmps1 = 1;
    googlePlayApp *tmp = appHash[slot];

    while(tmp && tmp->name != name)
    {
        cmps1++;
        tmp = tmp->next;
    }

    cmps += cmps1;

    if(tmp)
    {
        foundApp.name = appHash[slot]->name;
        foundApp.category = appHash[slot]->category;
        foundApp.rating = appHash[slot]->rating;
        foundApp.reviews = appHash[slot]->reviews;
    }
    else    return false;
}

this is my serach function and I'm trying to search if an app exists based on the data I stored from my code above. I'm trying to search it by the hash addresses, but it's not working...

0

There are 0 answers