Visual C++ 堆栈函数和错误处理
visual c++ stack functions and error handleing
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)
}
这是我用来弹出堆栈的代码,它有效,除非堆栈为空并且我尝试弹出它崩溃,我认为这是由于这一行顶部 = 顶部>下一个;
这是
你的代码清理了一点(只在需要时声明temp
并立即初始化,并防止使用else
子句处理空堆栈)。
void pop()
{
//if stack is empty, show message and stop
if(!top)
{
cout<<"nThe stack is empty!!!";
}
else //else stack is not empty, pop
{
PersonNode *temp = top;
top = top->next;
delete temp;
}
}
top=top->next;
如果top
NULL
,将失败,因此您应该简单地返回而不执行函数中的其余语句:
if(top==NULL) {
cout<<"nThe stack is empty!!!"; // show message
return; // <<<<<<<<<<<<<<
}
您应该在top
NULL
时返回
if(top==NULL)
{
cout<<"nThe stack is empty!!!"; // show message
return;//You Should return from here
}
希望这有帮助。
相关文章:
- 错误处理.将系统错误代码映射到泛型
- 通过错误处理,在C++中可靠地获得用户十六进制输入
- posix_spawn():使用posix_scawn()时的错误处理问题
- 在C++中样板"冷/never_inline"错误处理技术的最佳方法是什么?
- 关于 istream 中的错误处理的问题
- 程序使用的 C 库中的错误处理C++
- C++ 错误检查 fstream open() 命令和一般字符串流错误处理
- 使用std::tie进行类似golang的错误处理,同时返回结果,是否有缺点?(C++11)
- Boost进程"系统"功能中的错误处理
- RPN计算器c++错误处理和多个运算符
- 提升精神 x3 错误处理程序与期望
- 构造函数中的错误处理而不会失败
- 当 C++ 中函数参数的输入类型(类)错误时的错误处理
- 关于 ocilib 错误处理的问题,如何使用 ocilib 正确捕获日志错误?
- 我可以使用 std::optional 进行错误处理吗?
- C++ main() 末尾关于错误处理的错误
- 抽象包装带有异常的 C 错误处理的最佳方法
- vwprintf错误处理(ERRNO显示0)
- C++ 使用枚举进行错误处理
- 在 C++ 中创建自己的错误处理机制