具有链接列表的哈希表,仍然保存重复节点(C ++)

时间:2015-03-22 01:08:24

标签: c++ linked-list hashtable nodes

抱歉,如果我在呈现此方式时出错,但我的问题是在为链表创建新节点之后,然后它的第一个重复节点(由用户名判断)创建一个新节点但是所有其余的重复工作正常。 enter image description here

当要创建新节点时,将调用NEW NAME(Hashtable节点为NULL),然后在创建下一个条目后,它没有看到原始值我认为'if(temp-> userName) == tempUser)'会修复。任何帮助将不胜感激!

void linkedList::addNode(Node ** table, int hashLocation, string tempUser, string tempStart, string tempCPU, string tempPath, int tempPID)
{
    Node * temp = table[hashLocation];
    bool isTaken = false;

    if(temp != NULL)
    {
        cout << "TEMP'S USERNAME = " << temp->userName << endl;
        while (temp->next != NULL && isTaken == false) //&& tempUser != temp->userName)
        {
            cout << "USERNAME = " << temp->next->userName  << "TEMPUSER = " << tempUser << endl;
            if(temp->userName == tempUser) // PROBLEM HERE NOT BEING SET TO TAKEN
            {
                isTaken = true;
            }
            if(isTaken == false)
            {
                temp = temp->next;
            }
        }
        cout << "Exited loop" << endl;
        cout << "IS TAKEN = " << isTaken << endl;
        if(isTaken == true)
        {
            cout << "ITS TAKEN" << endl;
            string tempMinute, tempSecond;

            temp->processID.push_back(tempPID);

            istringstream iss(tempCPU);


            getline(iss, tempMinute, ':');
            getline(iss, tempSecond);

            temp->minuteCount.push_back(atoi(tempMinute.c_str()));
            temp->secondCount.push_back(atoi(tempSecond.c_str()));

            temp->pathName.push_back(tempPath);
            temp->timeStart.push_back(tempStart);
        }
        else
        {
            cout << "NOT TAKEN" << endl;
            temp->next = new Node(tempUser, tempStart, tempCPU, tempPath, tempPID);
        }
    }
    else
    {
        cout << "NEW NAME: " << tempUser << endl;
        table[hashLocation] = new Node(tempUser, tempStart, tempCPU, tempPath, tempPID);
        //cout << "TEMP'S USERNAME = " << temp->userName << endl;
    }
}

class Node
{
    public:
        Node();
        ~Node();
        Node(string tempUser, string tempStart, string tempCPU, string tempPath, int tempPID);
        Node * next;

        vector<int> processID, minuteCount, secondCount;
        vector<string> pathName, timeStart;
        string userName;

    private:


};

这是来自主

的电话
aList.addNode(aHash.getTable(), aHash.hashValue(tempUser), tempUser, tempStart, tempCPU, tempPath, tempPID);

我已经检查了它收到的值,它们都是正确的。

1 个答案:

答案 0 :(得分:1)

您没有检查列表中的最后一个元素,因为您正在检查temp->next不等于NULL而最后一个元素没有下一个元素。

将你的测试放在temp-&gt;接下来的循环中:

    while (isTaken == false) //&& tempUser != temp->userName)
    {
        cout << "USERNAME = " << temp->userName  << "TEMPUSER = " << tempUser << endl;
        if(temp->userName == tempUser) // PROBLEM HERE NOT BEING SET TO TAKEN
        {
            isTaken = true;
        }
        if(temp->next == NULL)
            break;
        if(isTaken == false)
        {
            temp = temp->next;
        }
    }

此外,输出temp-&gt; userName为cout,而不是temp-&gt; next-&gt; userName