带链表的哈希表,重复节点仍在保存(C++)
Hashtable with linked lists, duplicate nodes still saving (C++)
抱歉,如果我在展示这个的方式上犯了任何错误,但我的问题是在我为链表创建新节点之后,它是第一个重复节点(根据用户名判断)创建了一个新节点但是其余所有重复项都可以正常工作。
创建新节点时调用 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:
};
这是来自 main
的调用
aList.addNode(aHash.getTable(), aHash.hashValue(tempUser), tempUser, tempStart, tempCPU, tempPath, tempPID);
我检查了它接收到的值,它们都是正确的。
您没有检查列表中的最后一个元素,因为您正在检查 temp->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
抱歉,如果我在展示这个的方式上犯了任何错误,但我的问题是在我为链表创建新节点之后,它是第一个重复节点(根据用户名判断)创建了一个新节点但是其余所有重复项都可以正常工作。
创建新节点时调用 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:
};
这是来自 main
的调用aList.addNode(aHash.getTable(), aHash.hashValue(tempUser), tempUser, tempStart, tempCPU, tempPath, tempPID);
我检查了它接收到的值,它们都是正确的。
您没有检查列表中的最后一个元素,因为您正在检查 temp->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