简体   繁体   English

双链表,删除第一次出现

[英]Doubly Linked List, Removing First Occurrence

I am having trouble figuring out how to remove a first occurrence node. 我在弄清楚如何删除首次出现的节点时遇到了麻烦。

So far here is my code for removing the first occurrence node. 到目前为止,这里是我删除第一个出现节点的代码。

public boolean remove(E doomedElt)
{
    if (head == null)
        return false;

    else if (doomedElt.equals(head.data))
    {
        removeFirst();
        return true;
    }
    else
    {
        DLLNode<E> cursor = head;
        while(cursor.next != null && !cursor.next.data.equals(doomedElt))
            cursor = cursor.next;

        if (cursor.next.next == null && cursor.next.data.equals(doomedElt))
        {
            removeLast();
            return true;
        }
        else
        {
            cursor.next = cursor.next.next;
            cursor.next.next.prev = cursor.prev;  //<---Stuck here.
            cursor.prev = cursor.prev.prev;       //<---And here.

            return true;
        }

    }
}

Here is the code for removing last: 这是删除最后一个的代码:

public E removeLast()
{
    if (tail == null)
        throw new NoSuchElementException("Cannot removeFirst from empty list");
    else if (head == tail)
    {
        E firstOne = head.data;      
        head = tail = null;
        return firstOne;
    }
    else
    {
        E lastOne = tail.data;     
        tail = tail.prev;
        tail.next = null;
        return lastOne;
    }
}

Here is the code for removing first: 这是首先删除的代码:

public E removeFirst()
{
    if (head == null)
        throw new NoSuchElementException("Cannot removeFirst from empty list");
    else if (head == tail)
    {
        E firstOne = head.data;     
        head = tail = null;
        return firstOne;
    }
    else
    {
        E firstOne = head.data;     
        head = head.next;
        head.prev = null;
        return firstOne;
    }
}

When I run my driver, which will add (4, 5, 6 ,5, 7, 6) . 当我运行驱动程序时,它将添加(4, 5, 6 ,5, 7, 6) From there I tell it to remove the first 6 . 从那里我告诉它删除前6 What I should be getting is a (4, 5, 5, 7 ,6) following the .next link (which I do get), and following the .prev link I should be getting (6, 7, 5, 5, 4) . 我应该得到一个(4, 5, 5, 7 ,6)以下的.next的链接(这我做得到),并按照.prev链接我应该得到(6, 7, 5, 5, 4) Instead, I am getting (6, 7, 4) . 相反,我得到(6, 7, 4)

However, when I remove the cursor.next.next.prev = cursor.prev; 但是,当我删除cursor.next.next.prev = cursor.prev; and cursor.prev = cursor.prev.prev; cursor.prev = cursor.prev.prev; The .prev link goes back to the original but just backwards. .prev链接返回到原始链接,但仅向后。 Which means that my logic for reconnecting the .prev link in incorrect. 这意味着我重新连接.prev链接的逻辑不正确。

Can someone please help to with the logic for reconnect the . 有人可以帮忙重新连接逻辑吗? prev link, by bypassing the node. prev link,绕过节点。

This: 这个:

cursor.next = cursor.next.next;
cursor.next.next.prev = cursor.prev;  //<---Stuck here.
cursor.prev = cursor.prev.prev;       //<---And here.

Should be changed to this: 应更改为:

cursor.next = cursor.next.next;
cursor.next.prev = cursor;

Because cursor is the position before the element removed. 因为光标是元素删除之前的位置。 Ie if you have AB and C and you are removing B, the next of A should become C and the previous of C should become A. 也就是说,如果您有AB和C,并且要删除B,则A的下一个应成为C,C的前一个应成为A。

Note this code is untested but it should work - let me know if not and I will help more. 请注意,此代码未经测试,但可以正常工作-如果没有,请告诉我,我们将为您提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM