删除循环双向链表中的节点
Removing a node in a circular doubly linked list
我正在尝试编写一些代码来从循环双向链表中删除节点。我编写了以下函数,它主要有效:
bool Circular::remove(int index)
{
if (head == NULL)
return false;
Node* current;
current = head;
if (index > 0)
{
for (int i = 0; i < index; i++)
{
current = current->next;
}
}
else if (index < 0)
{
for (int i = 0; i < abs(index); i++)
{
current = current->prev;
}
}
if (current == head)
{
head = current->next;
}
else if (current == tail)
{
tail = current->prev;
}
current->prev->next = current->next;
current->next->prev = current->prev;
return true;
}
我唯一的问题是,当我将数字 1 传递到索引号中时,它不会删除正确的值。相反,它总是删除尾巴。如果您认为我的代码在其他地方有问题,那么我也会调查一下。
我想
我已经想通了。大多数情况下,我使用功能来移除头部...
Node* temp = head;
head = head->next;
head->prev = tail;
tail->next = head;
delete temp;
return true;
。并移除尾巴:
Node* temp = tail;
tail = tail->prev;
tail->next = head;
head->prev = tail;
delete temp;
return true;
显然,仅仅在头部和尾部移动并不足以实际删除这些节点。
相关文章:
- 为什么我的双向链表删除函数会删除多个节点?
- 在C++中删除双向链表的头节点后出现访问冲突异常
- 双向链表 - 无法删除第一个节点
- 双向链表 std::unique_ptr 类在节点删除时无法按预期工作
- 在给定节点之后插入双向链表中的节点
- 在双向链表中给定节点之后删除节点
- 尝试删除所选节点的双向链表
- 双向链表插入方法实现 - 正在搜索哪个节点
- 为什么我的函数没有打印出双向链表中的第一个节点?
- 使用类从双向链表中删除节点
- 从双向链表末尾删除节点时出现分段错误
- 删除双向链表 (C++) 中的节点
- 使用双向链表中的指针交换节点
- 在双向链表中插入节点
- 删除双向链表 (C++) 的最后一个节点
- 在循环双向链表的第一个节点之前/之后插入的算法是什么?
- 在双向链表的中间插入一个节点
- 删除循环双向链表中的节点
- 双向链表节点的下一个是私有的
- 双向链表节点交换问题