如何替换链表中某个节点的数据
How to replace data in a certain node in a linked list?
我在课堂上有一个使用链表的练习。我对这门语言相当陌生,但我已经试过了。指令告诉我们"迭代直到找到NodeData,然后使用集合替换数据"。c++中的"集合"是什么?我在网上找过了,什么也找不到。我唯一能想到的就是把节点指向别的地方。例如:head->NULL
。但是,如果我只是简单地替换数据,这真的有必要吗?为了替换数据,我尝试了temp->order = NEWDATA
。这是正确的实现吗?这似乎并不奏效。可能是代码中其他部分的错误。
bool OrderLL::Modify(Order * NodeData) {
OrderNode *temp = head;
if (temp == NULL) {
cout << "Empty List";
}
else {
while (temp != NULL) {
if (temp->order == NodeData) {
//not sure if this is the proper way of deleting data inside a node
delete anOrder;
//HOW DO I REPLACE THE DATA IN THIS PART?
}
temp = temp->next;
}
}
return false;
}
顺便说一句,我真的不明白为什么我所有的问题都一直收到不满意的投票。是因为它们是基本的c++问题吗?它们对我来说不是很基本。我知道这个网站看不起"跑题/聊天讨论",但我只是不明白我的问题出了什么问题。
您在问题中提到了"replace",所以只是猜测,但可能您希望替换节点本身,而不仅仅是数据。在这种情况下,它会像这样
if(curr_node->data == to_replace_data){
curr_node->next = new_node;
new_node->next = curr_node->next->next;
free( curr_node->next); //or return curr_node->next depending on what
// you are trying to do.
}
相关文章:
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 如何在Visual C+++中从JSON文件中读取节点数据
- 修改链表主函数代码,用户将在其中输入节点的索引和数据以及正确的消息
- 如何在C++中实现节点数任意的通用树数据结构?
- 如何在C++中使用带有SFML的http reqest从节点.js服务器获取数据?
- 如果我们不创建一个新节点并使用指针插入数据并建立链接(在链表中)怎么办?
- 使用 Broadcast 发出的从节点服务器发送的数据不能被 C++ 套接字 IO 客户端读取
- C++ Eclipse:二叉搜索树节点>数据=变量似乎不起作用
- C++ 在节点内存储对数据对象的引用,而不复制数据
- 如何通过其持有的数据从链表中删除某个节点?
- 基于给定字符串数据类型的链表删除节点
- 为什么我的节点临时节点不能显示正确的数据?
- C++中的数据结构,插入链表中节点的开头
- 从链表数组访问节点数据
- 如何取消引用作为指针的链表节点数据
- 如何将XML节点数据转换为QT中的字符串
- 无法访问类中的节点数据
- C++ 使用 std::p air 模板专用化定义树节点数据结构
- 如何使用pugixml解析获得具有相同节点名称的节点的节点数据
- 如何添加备用节点数据