class stack_class
{
private:
struct stack_struct *head;
public:
stack_class();
~stack_class();
void pushNumber(int number);
void popNumber();
void findNumber();
void clearStack();
void sizeFinder();
void printStack();
};
void stack_class::popNumber()
{
stack_struct *pointerPop=NULL,*pointerPop2=NULL;
int popCounter=0,i=0;
pointerPop2=tailPointer;
if(head==NULL)
{
cout<<"\nNo Member to Delete.\n";
}
else
{
while(pointerPop2)
{
popCounter++;
//cout<<pointerFunc3->number<<endl;
pointerPop2=pointerPop2->next_number;
}
pointerPop=tailPointer;
while(i<(popCounter-2))
{
pointerPop=pointerPop->next_number;
i++;
}
pointerPop->next_number=NULL;
delete head;
head=pointerPop;
}
}
void stack_class::printStack()
{
pointerFunc3=tailPointer;
if(tailPointer==NULL)
{
cout<<"\nNo Members in List.\n";
}
else
{
cout<<"\n\nList Is:\n";
while(pointerFunc3)
{
cout<<pointerFunc3->number<<endl;
pointerFunc3=pointerFunc3->next_number;
}
}
}
构造函数到类
stack_class::stack_class()
{
head=NULL;
}
这是我的代码,问题是,当我弹出最后一个数字并尝试打印列表时,它进入无限循环并打印垃圾。当我删除列表中的所有内容后按删除选项时,程序会冻结。有什么建议为什么这样做?我该如何解决?
答案 0 :(得分:0)
你的第一个while循环的条件永远不会是假的。根据你实现next_number的方式(如果它是循环链表),我相信你应该把它改成:
while(pointerPop2 != head)
如果你想让它遍历所有节点。