散列,无法插入到散列表

时间:2019-11-12 22:47:06

标签: hashtable

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++;
    }

我的程序在while循环的第65次迭代上停止。 appInsert呼叫的第64位... 为什么我不能使它正常工作? 这是一个程序,它读取数据文件并将其存储在哈希表中,并通过开放式寻址处理冲突。...

更新的问题

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;
}

这是我的serach函数,我正在尝试根据上面代码存储的数据来搜索是否存在应用。我正在尝试通过哈希地址搜索它,但是它不起作用...

0 个答案:

没有答案