在列表中搜索数据的最后一个实例
Searching for the last instance of data in a list
我有一个赋值来编写一个函数,该函数在列表中搜索数据的最后一个实例(在本例中为整数)。该函数在if语句行中因访问冲突而中断。
Node* List::SearchLast (int val)
{
Node* pLast=NULL;
Node* pNode=pHead;
while (pHead!=NULL)
{
if (pNode->data==val)
pLast=pNode;
pNode=pNode->next;
}
return pLast;
}
我试着观察pNode发生了什么。这里它变成了0。然后传递while语句。我做错了什么?
您的while
是一个无限循环,更改为:
Node* List::SearchLast(int val)
{
Node *pLast = NULL;
Node *pNode = pHead;
while (pNode != 0) {
if (pNode->data == val) pLast = pNode;
pNode = pNode->next;
}
return pLast;
}
相关文章:
- lower_bound()返回最后一个元素
- 获取向量C++中第一个值和最后一个值的和
- 获取用C/C++打印的最后一个字符串
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- 为什么这个程序返回最后一个单词而不是最长的单词?
- 为什么我的最后一个 ELSE 条件无法正确执行
- 如何将模板的实例传递给另一个模板的另一个实例?
- Lower_bound不适用于具有 3 个元素的向量的最后一个元素
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- 仅显示链表的最后一个元素
- 测试迭代器是否位于列表中的最后一个
- push_back通过自行创建的对象获取最后一个元素的向量
- 使用 map.end() 访问 map 的最后一个元素
- 如何知道地图中的最后一个元素是否被删除?
- std::矢量保存 只推送最后一个元素
- 在类的第一个/最后一个实例存在之前/之后调用一对函数.有没有更好的方法?
- 从字符串中删除字符的第一个和最后一个实例
- C 是否有办法仅返回for循环的最后一个实例
- 当类的最后一个实例被破坏/覆盖时调用函数
- 在列表中搜索数据的最后一个实例