[英]Delete in circular doubly linked list connected with head
我有两个圆形的双向链表,它们分别与head和integer元素连接(无序)。 要删除的第一个列表中包含第二个列表中的值。 指针如何工作? 如何排除在外? 是否需要在第一个列表中搜索值以删除第二个列表? 我如何与他们合作? 可以解释该算法的操作来解决它吗?
范例:
我有两个圆形的双头链表。
L1:40 100 90 20 10 32 66
L2:60 10 46 30 80 90
我想在第一个列表中删除第二个中的值。 然后,第一个列表将是:
L1:40 100 20 32 66
我想知道如何使用列表的指针进行排除。 我想要一个伪代码概念。 我已经创建了用C编写代码,但是我不理解该算法。 我需要先了解如何做。
首先编写算法的伪代码,然后实现可以独立测试的实际功能。
一般的伪代码如下所示:
for each node in list1
{
if (list2 contains node)
{
remove node from list1
}
}
假设您的列表和节点定义如下:
struct Node
{
struct Node *next;
struct Node *prev;
int number;
}
struct List
{
struct Node *head;
}
// these should be instantiated somewhere
struct List* list1;
struct List* list2;
因此,该函数的框架类似于:
struct Node* node = list1->head;
while (node != null)
{
// prepare the next node early
struct Node* next = node->next;
// check if list2 contains a matching node
if (match(list2, node->number))
{
// remove the node properly,
// updating whatever you need to update
remove(list1, node);
}
// if it's circular, check if this
// is the last node
if (next == list1->head)
break;
node = next;
}
因此,现在只剩下实现两个功能了:
// returns true if any node in list contains the specified number
bool match(struct List* list, int number);
// removes the node from the list
void remove(struct List* list, struct Node* node);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.