c++ - 如何从两个不同的双向链表中更改两个不同的节点?
问题描述
我在节点中有2个双向链表,我比较节点的数据,如果第二个列表的节点的数据大于第一个列表的节点的数据,我将切换节点。我怎样才能做到这一点。
这将删除旧节点并添加给定的节点。
void DoubleLinkedList::SwitchNodesFromList(Node* changeNode, int index)
{
RemoveAt(index);
if (index < 0 || index > size)
throw "Error";
if (index == 0)
{
head = changeNode;
if (head->next != NULL)
head->next->prev = head;
}
else
{
Node* prv = findPreviousByPosition(index);
prv->next = changeNode;
if (prv->next->next != NULL)
prv->next->next->prev = prv->next;
}
}
保持节点这些会改变
void ListController::SwitchNodes(Node* firstNodeOfList, Node* secondNodeOfList, int index)
{
Node* first = firstNodeOfList;
Node* second = secondNodeOfList;
Node* temp = first;
first->next = second->next;
first->prev = second->prev;
second->next = temp->next;
second->prev = temp->prev;
firstList->SwitchNodesFromList(second, index);
secondList->SwitchNodesFromList(first, index);
delete temp;
}
我比较项目,但我不能切换节点
void ListController::CompareItemsOfNodes()
{
for (int i = 0; i < firstList->Count(); i++)
{
if (firstList->ElementAt(i) > secondList->ElementAt(i))
{
ReverseList(firstList);
}
else if (firstList->ElementAt(i) == secondList->ElementAt(i))
{
ReverseList(secondList);
}
else if (firstList->ElementAt(i) < secondList->ElementAt(i))
{
SwitchNodes(firstList->GetNode(i), secondList->GetNode(i), i);
}
}
}
解决方案
推荐阅读
- javascript - 当angularjs中需要字段时如何显示红色顺序
- javascript - Javascript fetch 中 URL 中异步函数的值
- c# - 使用 C#/LinQ 删除列表中的重复字符串但忽略大小写
- graphql - GraphQL-此模式中的请求数
- python - 我如何检查两个 request.form 元素以查看它们在 python 中是否相等?
- excel - 在 2 或 3 页上运行宏
- xml - 如何对具有 XMLType 列的表使用 Order by、Group by、Count() 等操作?
- jquery - Json 响应数据获取到 html 表
- java - 使用 SSL 的 Nexus Repository Manager 3 浏览器或 Docker 无法访问
- java - 在 8 谜题游戏中获得继任者