使用双指针在链接列表中插入节点

时间:2016-06-29 14:49:10

标签: c++ linked-list singly-linked-list double-pointer

我最近看到了使用双指针从单个链表中删除节点的实现。除了使代码更美观之外,这种实现还有效率方面的任何好处。另外,我如何实现类似的方法将节点插入链表(不跟踪以前的节点)。我真的很好奇,如果有更好的算法来实现这个

Node* Delete(Node *head, int value)
{
    Node **pp = &head; /* pointer to a pointer */
    Node *entry = head;
    while (entry ) 
    {
        if (entry->value == value)
        {
            *pp = entry->next;
        }
        pp = &entry->next;
        entry = entry->next;
    }
    return head;
}

2 个答案:

答案 0 :(得分:0)

  

除了使代码更美观之外,还有这个实现   有效率的任何好处。

没有什么可以比较的,所以很难说,但这与从链表中删除节点的效率差不多。请注意,函数名称Delete将更准确为Remove,因为它实际上不会清除它从列表中删除的节点。

  

另外,我如何实现类似的方法来插入节点   到链接列表(不跟踪以前的节点)。

一种方法是展望未来。最好按照删除功能的格式显示示例。

void insert(Node *head, int value)
{
    Node *entry = head;
    while (entry)
    {
        if (entry->next == NULL)
        {
            entry->next = new Node(NULL, value);
            return;
        }
        else if (value < entry->next->value)
        {
            entry->next = new Node(entry->next, value);
            return;
        }
        entry = entry->next;
    }
}

答案 1 :(得分:0)

为了插入到只存储头部的列表的后面,没有尾部(这意味着可以接受线性时间插入的小列表),你可以通过引入额外的指针间接来消除特殊情况:< / p>

简单版本(指向节点的指针)

void List::push_back(int value)
{
    // Point to the node link (pointer to pointer to node), 
    // not to the node.
    Node** link = &head;

    // While the link is not null, point to the next link.
    while (*link)
        link = &(*link)->next;

    // Set the link to the new node.
    *link = new Node(value, nullptr);
}

......你可以减少到:

void List::push_back(int value)
{
    Node** link = &head;
    for (; *link; link = &(*link)->next) {}
    *link = new Node(value, nullptr);
}

相反,说:

复杂版本(指向节点的指针)

void List::push_back(int value)
{
    if (head)
    {
        // If the list is not empty, walk to the back and
        // insert there.
        Node* node = head;
        while (node->next)
             node = node->next;
        node->next = new Node(value, nullptr);
    }
    else
    {
        // If the list is empty, set the head to the new node.
        head = new Node(value, nullptr);
    }
}

或者公平地删除评论:

void List::push_back(int value)
{
    if (head)
    {
        Node* node = head;
        for (; node->next; node = node->next) {}
        node->next = new Node(value, nullptr);
    }
    else
        head = new Node(value, nullptr);
}

简单版没有特例

第一个版本没有特殊情况空列表的主要原因是因为如果我们想象head为空:

Node** link = &head; // pointer to pointer to null
for (; *link; link = &(*link)->next) {}
*link = new Node(value, nullptr);

然后for循环条件立即为假,然后我们将新节点分配给head。当我们使用指针指针时,我们不必在循环外单独检查这种情况。

插入排序

如果你想进行插入排序而不是简单地插入后面,那么:

void List::insert_sorted(int value)
{
    Node** link = &head;
    for (; *link && (*link)->value < value; link = &(*link)->next) {}

    // New node will be inserted to the back of the list
    // or before the first node whose value >= 'value'.
    *link = new Node(value, *link);
}

<强>性能

至于性能,不确定消除额外分支有多大区别,但它肯定会使代码更紧凑并降低其圈复杂度。 Linus认为这种风格是“好品味”的原因是因为在C中,你经常需要经常编写链表逻辑,因为它不是那么容易而且必然要概括链表,因为我们没有类模板,例如,所以它是方便地支持更小,更优雅,更不容易出错的方式来编写这些东西。此外,它表明你很好地理解指针。