为什么一个循环需要电流>链接 !=NULL 和一个电流 != NULL 作为我的链表?
Why does one loop require current->link !=NULL and one current != NULL for my linked list?
我为一个学校项目写了几个函数,我让它们工作,但我不明白为什么。 它们应该是相同的,但只有当一个检查当前>链路而另一个检查当前本身时,它们才有效。两个循环不应该都是当前的!= NULL?
这两个函数在 main 中调用如下:
customerHead = fillCart(limit, lowLevelInv); //fills linked list
totCart(customerHead, lowLevelInv);
printCart(customerHead, lowLevelInv);
这仅在 while 循环检查电流 != NULL 时才有效
int totCart(OrderPtr head, inventory lowLevelInv[])
{
OrderPtr current;
current = head;
int tot = 0;
while(current != NULL)
{
tot += lowLevelInv[current->itemID].cost*current->qtyReceived;
current = current->link;
}
cout<<"Cart total is: "<<tot<<endl;
return tot;
}
这仅在 while 循环检查当前>链路时有效!=NULL
void printCart(OrderPtr head, inventory lowLevelInv[])
{
OrderPtr current;
current = head;
cout<<"you have ordered: n";
while(current->link != NULL);
{
cout<<current->orderID<<": "<<current->qtyReceived<<" " <<lowLevelInv[current->itemID].name<<" for "<<lowLevelInv[current->itemID].cost*current->qtyReceived<<endl;
current = current->link;
}
}
看起来问题就在这里:
while(current->link != NULL);
{
cout<<current->orderID<<": "<<current->qtyReceived<<" " <<lowLevelInv[current->itemID].name<<" for "<<lowLevelInv[current->itemID].cost*current->qtyReceived<<endl;
current = current->link;
}
如果你仔细观察,你会发现在"while"语句的控制语句后面有一个虚假的分号。这意味着如果当前>链接不为空,您的程序将挂起,因为没有任何内容会更改当前或当前>链接。
如果这实际上不是您的问题(例如,由于复制意大利面问题),您应该向我们展示您如何构建列表以及您所说的"不起作用"的具体含义。
相关文章:
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 为什么在C++中增加指针后打印了一个值而不是 NULL/0?
- 我正在尝试计算 char 数组中的内容,直到 null 终止,但每次编译时,我都会得到一个比我的数组大的数字
- [C ] NULL确实在内存中也包含一个特定的地址
- 链表:最后一个指针不是NULL
- 移动语义是否只是一个浅层副本并将其他人的指针设置为 null?
- 为什么一个指向char的C++指针在缺少null终止符的情况下充当字符串
- 为什么一个循环需要电流>链接 !=NULL 和一个电流 != NULL 作为我的链表?
- 当通过其中一个指针删除对象时,C++将所有指针设置为null
- std::string::c_str() 是否总是返回一个以 null 结尾的字符串
- 在 c++ 中抛出一个值(如果该值为 null)
- strtok() - 为什么必须传递 NULL 指针才能获取字符串中的下一个标记
- 在读取 Visual C++ 中的二进制文件(而不是其他编译器)后,每隔一个字符都是 NULL
- Winsock2 tcp/ip-一些数据包被忽略,可能是由于前一个数据包的null终止符
- 如何生成一个以非null结尾的c字符串
- 生成一个随机种子给srand()-不是时间(NULL)
- 如何使用 **P 创建一个指针数组并将其指向 NULL
- 您可以将一个指针设置为另一个值为 NULL 的指针吗?
- 为什么我们需要argc,而argv的末尾总是有一个null
- 从技术上讲,在strack跟踪中有一个null this指针是怎么可能的