我正在编写一个添加、删除和显示节点(双链接)及其组件的程序,但每当我试图检索节点并显示其组件时,我都会收到以下错误:
2 [main] a 4640 exception::handle: Exception: STATUS_ACCESS_VIOLATION
2875[main]a 4640 open_stackdumpfile:将堆栈跟踪转储到.exe.stackdump
我已经将其缩小到我的.h文件中的搜索功能,该功能应该进行搜索,以查看链接列表中是否有正在搜索的帐号的节点。函数返回在它之前的节点,或"上一个"节点。
这是我的搜索功能:
bool searchListByAcctNum (int searchKey, nodePtr *prevOut)
{
bool found = false;
nodePtr p = headNum;
nodePtr prev = NULL;
while (p != NULL)
{
if (p->acctNum < searchKey)
{
prev = p;
p = p->nextNum;
}
else
{
if (p->acctNum == searchKey)
found = true;
p = NULL;
}
}
*prevOut = prev;
return found;
如果有人能帮助我,我将不胜感激!
看起来您的列表可能已损坏,或者您为接收上一个节点而传递的指针无效,因为该代码看起来不错。然而,在我看来,它可以用一种更简单的方式编写:
bool searchListByAcctNum (int searchKey, nodePtr *prevOut) {
/// Start at beginning of list, use pointer variable to hold previous.
nodePtr p = headNum;
*prevOut = = NULL;
// Process entire list, will exit early if need be.
while (p != NULL) {
// If past it, just return false, caller should ignore prevOut.
if (p->acctNum > searchKey)
return false;
// If equal, return true, prevOut holds previous or NULL if found at start.
if (p->acctNum == searchKey) {
return true;
// Save previous and advance to next.
*prevOut = p;
p = p->next;
}
// Reached end of list without finding, caller should ignore prevOut.
return false;
}