void pop()
{
//create new temp node
struct PersonNode *temp;
//if stack is empty
if(top==NULL)
{
cout<<"nThe stack is empty!!!"; // show message
}
temp=top; // store the top at temp
top=top->next; // make the top previous to current top
delete temp; // delete the temp (current top)
}
这是我用来弹出堆栈的代码,除了当堆栈是空的,我尝试弹出它崩溃,我认为它由于这条线 top = top-> next;如果top
是NULL
C++堆栈函数和错误处理
如果你仍然做同样的事情,那么NULL检查是什么? – Michael
你不需要'struct PersonNode * temp;'中额外的'struct',并且应该将它移到实际分配它的地方(或初始化为'nullptr')。 – crashmstr
此外,您还需要一个'else'(如果堆栈为空,您*不会做出更改)。 – crashmstr