所以你们都知道这是一个家庭作业问题而且我花了好几个小时试图弄清楚这件小事。我的pop()
无效。这是我pop()
的当前代码:
StackElement Stack::Pop(){
Position temp;
temp = stack.CurrentEntry();
stack.Delete();
return temp;}
我正在使用Delete()
这是一个基于delete()
功能的链接列表,如下所示:
void LinkedList::Delete(){
if (current == first && AtEnd()){
// Remove the memory allocation for the element's data
delete ¤t->data;
// Reset all values of the linked list to original (null) state
current = NULL;
pred = NULL;
first = NULL;
}
else
// Checks if the current element is the first node in the lists
if (current == first){
// Make new first element to be the next element
first = current->next;
// Remove the memory allocation for the element's data
delete ¤t->data;
// The new current entry is the successor of the deleted node.
current = first;
}
// When the element you're deleting is not the first node in list
else{
assert(!Empty());
// Temporary node to prevent current from being marroned
Node *tempNode = current->next;
pred->next = tempNode;
// Remove the memory allocation for the element's data
delete ¤t->data;
current = tempNode;
}
}
当我编译时,它会在这里抛出这个错误:
Program5_test.exe中0x003E5F79处的未处理异常:0xC0000005:访问冲突写入位置0xF495EE12。
非常感谢任何帮助。
答案 0 :(得分:0)
你没有向我们展示CurrentEntry,但我敢打赌它只是返回一个指针,而不是制作一个完整的副本。然后你删除原来让指针指向任何东西。当您使用从Pop返回的指针时,它会崩溃。