【问题标题】:Hashtable with linked lists, duplicate nodes still saving (C++)带有链表的哈希表,重复节点仍在保存(C++)
【发布时间】:2015-05-25 05:45:35
【问题描述】:

对不起,如果我在呈现这个的方式上犯了任何错误,但我的问题是在我为链表创建新节点之后,它是第一个重复节点(由用户名判断)创建了一个新节点,但所有其余的副本工作正常。

当要创建一个新节点时调用新名称(哈希表节点为 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:


};

这是来自 main 的调用

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

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

【问题讨论】:

  • 不相关:每次在问题中发布图像代码或终端输出时,都会有一只小猫死亡。

标签: c++ linked-list hashtable nodes


【解决方案1】:

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

将 temp->next 测试放在循环中:

    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->userName 到 cout,而不是 temp->next->userName

【讨论】:

  • 非常感谢您,包括评论后的额外帮助,干得好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 2015-07-18
  • 2019-09-22
  • 1970-01-01
相关资源
最近更新 更多