链接列表中最后一项之后的项目是否为0
Does the item after the last item in a linked list have the value of 0?
我正在为作业写快速排序算法,但我想知道我的循环是否会太早停止。循环会超越最后一个元素并停止,还是会继续读取记忆中留下的随机数?这有意义吗?
void ListP::partition(ListNode * &Larger, ListNode * &Smaller, ListNode * &pivot ){
ListNode *curr, *temps, *templ; *temps= *templ = NULL;
curr=pivot->next;
pivot->next=NUll;
while(curr!=NULL){
if (curr->item<=pivot->item){
if(Smaller==NULL){
Smaller=curr;
curr=curr->next;
Smaller->next=NULL;
temps=Smaller;
}
else{
temps->next=curr;
curr=curr->next;
temps=temps->next;
temps->next=NUll;
}
}
else{
if(Larger==NULL){
Larger=curr;
curr=curr->next;
Larger->next=NULL;
templ=Larger;
}
else{
templ->next=curr;
curr=curr->next;
templ=templ->next;
templ->next=NUll;
}
}
}
}
链接列表中最后一项之后的项目是否为0?
根据定义,最后一项之后没有项目。如果最后一项之后有一项,那么以前的项目将不是最后一个。
我想知道我的循环是否会太早停止。 while循环会超越最后一个元素
快速浏览一下,循环的所有分支do curr=curr->next
,循环终端条件为 curr!=NULL
,因此循环将在next
为null的节点后结束。如果列表的最后一个节点的next
指向null,则循环不会超越它。在最后一项之前,节点的next
不能指向null,因此循环也不应太早结束。
但是,如果列表的最后一项的 next
并未指向null,则循环不会在该节点上结束。
您应该验证程序的行为是您期望通过使用调试器行为的方式。
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 从链接列表c++中删除一个项目
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 有没有一种简单的方法可以在C++中获取特定索引之后向量中的所有项目?
- 移动了 QT 项目,重命名文件夹和项目名称,Moc'ing 返回"'-I'之后缺少值"
- 为什么应该在每个项目包配置之后包含(CPack)?
- 我正在尝试在Eclipse中构建一个C 项目,但是即使在使用MINGW路径设置路径变量之后,也会获得构建错误
- 链接列表中最后一项之后的项目是否为0
- 在x元素之后,从QT中删除x元素之后的文件夹中的项目
- unordered_map仅查找先前找到的项目之后的项目
- 在 Items.ItemAdd 事件之后获取邮件项目
- 项目.exe在系统("pause")之后触发了断点
- 欧拉项目任务#8,代码在某个点之后开始返回错误的答案
- 在C++推送到队列后,如果我在之后编辑该项目数据,它会影响队列中的数据