简体   繁体   English

从循环双链表(Java)中删除节点?

[英]Removing nodes from a circular doubly linked list (Java)?

I am writing some code where I need to remove an item in a circularly linked list (who's head acts as a dummy node) and return it (if removing the first node). 我正在编写一些代码,在其中我需要删除循环链接列表(其头部充当虚拟节点)中的项目并返回它(如果要删除第一个节点)。 I think I have the code just about right, but I'm not sure. 我认为我的代码差不多正确,但是我不确定。

Am I understanding this correctly? 我理解正确吗? (starting with a dummy node) (从虚拟节点开始)

dummy -> A -> B -> C -> D -> dummy (wrap around to the dummy node) 虚拟-> A-> B-> C-> D->虚拟(环绕到虚拟节点)

So if I want to remove the first actual piece of data(A), I would need to assign it to a temp variable. 因此,如果要删除第一个实际的数据(A),则需要将其分配给temp变量。 So Node first = head.next. 因此,节点优先= head.next。 Then I need to have the dummy head reference "b" so I would need to do head.next = first.next. 然后,我需要使用虚拟头部引用“ b”,所以我需要做head.next = first.next。 Is this all that needs to be done? 这是所有需要做的吗?

private Node remove()
{
     Node returnNode = head.next;
     head.next = returnNode.next;
     return returnNode;
}

In the case of removing any node N from the list (assuming it is on the list), it is sort of the same concept? 在从列表中删除任何节点N的情况下(假设它在列表中),是否具有相同的概念? So from the example above, lets say we want to remove node B. In this case, we need to set B.next = B.previous and B.previous = B.next correct? 因此,从上面的示例中,我们要删除节点B。在这种情况下,我们需要将B.next = B.previous和B.previous = B.next设置正确吗? Or do I need to do something like B.previous.next = B.next and B.next.previous = B.previous? 还是我需要做类似B.previous.next = B.next和B.next.previous = B.previous的事情? Do I need to traverse the list to find the element to remove? 我是否需要遍历列表以找到要删除的元素?

private void removeNode(Node n)
{
    n.next = n.previous; // or n.previous.next = n.next
    n.previous = n.next; // or n.next.previous = n.previous
}

A double linked list means that each node has also a connection to the previouse node. 双重链接列表意味着每个节点还具有到先前节点的连接。

Your example would only switch the next and previous references. 您的示例将仅切换下一个和上一个引用。 You should set: 您应该设置:

n.next.previous = n.previous n.previous.next = n.next n。上一个= n。上一个n。上一个

Since you have a circular linked list there should only be special cases when adding the first element or removing the last element. 由于您具有循环链接列表,因此只有在添加第一个元素或删除最后一个元素时才有特殊情况。

In any case you need to traverse the list to remove or add, whether you are using single linked list or doubly linked list or circular LL. 无论使用单链表,双链表还是循环LL,无论如何都需要遍历列表以进行删除或添加。

Now as per your question, you haven't mentioned whether it is a doubly LL or not but I am assuming it is as you have used node.previous in your second example. 现在根据您的问题,您没有提到它是否是双倍LL,但我假设它与您在第二个示例中使用的node.previous一样。

As per my understanding if you are having a node of circular LL, You don't have to worry about whether this node is head or not because anyhow all the nodes will be accessible, using this node. 根据我的理解,如果您有一个循环LL的节点,则不必担心该节点是否为头节点,因为无论如何,使用此节点都可以访问所有节点。

Now If I am able to understand your problem correctly, then for your first example, 现在,如果我能够正确理解您的问题,那么对于您的第一个示例,

if you need to delete a node(A in this case), u need that node in your function parameter. 如果您需要删除节点(在这种情况下为A),则需要在功能参数中删除该节点。 Something like this... 像这样

// assuming the key exists in your LL.

    private Node remove(Node nodeToBeRemoved)
    {   
        Node returnNode = nodeToBeRemoved
        while(currentNode.data = nodeToBeRemoved.data) {
            returnNode = returnNode.next
        }
        returnNode.previous.next = returnNode.next
        return returnNode;
    }

Same should be the case for the second example as well. 第二个示例也应如此。

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

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