只是为了让你们都知道这是一个家庭作业问题,我花了几个小时试图弄清楚这件小事。我的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的访问冲突。
您没有向我们显示CurrentEntry,但我敢打赌它只是返回一个指针,而不是完整复制。然后删除原始文件,使指针不指向任何内容。当您使用从Pop返回的指针时,它会崩溃。