Stack.Pop() using LinkedList
Stack.Pop() using LinkedList
只是为了让你们都知道这是一个家庭作业问题,我花了几个小时试图弄清楚这件小事。我的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返回的指针时,它会崩溃。
相关文章:
- C++:正在检查LinkedList中的回文-递归方法-错误
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- "using namespace std;"在C++的作用是什么?
- 在c++中实现LinkedList时,应出现未处理的错误
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 已修改的LinkedList未在文本文件本身中更新
- vector<vector<double>> to mxArray using memcpy
- Using C++ CryptAPI:CNG Create CSR
- STLPort using C++11
- 函数中的 c++ using 语句,后跟函数名称(对于 ADL?
- Using getchar_unlocked()
- 类作用域的类型别名"using":[何时]方法中的用法可以先于类型别名?
- 嵌套命名空间的"using"指令,但需要命名内部命名空间
- 为什么"using System;"不被视为不良做法?
- 为什么我需要做'using namespace std'而不是"使用 std::cout"?
- 'using namespace'实现细节的便捷方法(仅标头库)?
- 如何在 c++ 中处理 LinkedList 的指针
- C++中"typedef"、"using"、"namespace"和"using namespace"有什么区别?
- 在"template"和函数声明之间使用:template<typename trait> using tr = base_trait<trait> void fn(tr::t
- Stack.Pop() using LinkedList