【发布时间】:2021-01-22 21:00:41
【问题描述】:
此函数在 LinkedList 中创建,用于修改给定位置的节点。但是,这个函数不能正常工作,它给出了一些随机值。
void update_data(int old, int new_data) {//Function toupdate node
Node *curr=header;//Data members
int pos = 0;
while(curr->next!=NULL) {
if(curr->isbn == old)
{
curr->isbn = new_data;
cout<<old<<" Found at position "<<pos<<" Replaced with "<<new_data<<endl;;
}
curr = curr->next;
pos++;
}
}
【问题讨论】:
-
是时候开始调试了!祝你好运!
-
@M. Ameen Akbar 没有使用变量 pos。
-
@M. Ameen Akbar 你说这个功能不能正常工作是什么意思?
-
如果列表为空且
curr为NULL,请考虑while(curr->next!=NULL)处发生的情况。 -
与您的Rubber Duck 坐下来讨论是否值得在找到并更新项目后继续搜索列表。
标签: c++ oop linked-list singly-linked-list function-definition