简体   繁体   English

如何在第一个元素之后实现双向链表

[英]How to implement Doubly Linked List after the first element

I'm trying to create a double linked list in Java.我正在尝试在 Java 中创建一个双链表。 With the following elements inside each node:每个节点内包含以下元素:

public class Node {
    int data;
    Node next;
    Node before;
}

data is just a number (0 to 9). data只是一个数字(0 到 9)。 Node next indicates the next pointer in a list, and Node before the before pointer in a list. Node next表示列表中的下一个指针,而Node before列表中的 before 指针之前。

In the first loop where i ask for list == null is the first element in a list, which i indicate that the new.before = null so its the first element in the linked list.在第一个循环中,我要求list == null是列表中的第一个元素,我表示new.before = null因此它是链表中的第一个元素。 And the new.next=list so it can continue building the list without entering the loop again.new.next=list这样它就可以继续构建列表而无需再次进入循环。

But i'm stuck when i try to link the other elements in the list, not the first one .但是当我尝试链接列表中的其他元素而不是第一个元素时,我被卡住了。 I have the idea which i need to link the element before and the next element.我有一个想法,我需要链接之前的元素和下一个元素。 But i dont know how to implement this idea.但我不知道如何实现这个想法。

This is the code for generating the doubly linked list.这是生成双向链表的代码。

public static Node CreateDoubleLinkedList(Node list, int data)
    {
        Node nuevo=new Node();
        new.data=data;
        new.next=null;
        new.before=null;
        Node aux=list;
        
        if(list==null)
        {
            list=new;
            new.next=list;
            new.before= null;
            return list;
        }
        else
        {
            /* I dont know how to implement the idea of the doubly linked list after the first element*/
            new.before=aux;
            while(aux.next!=null)
            {
                aux=new.next;
            }
            return list;
        }   
    }

Okay, so when attaching nodes to a doubly-linked list there are more links, and therefore more cases to be concerned about.好的,所以当将节点附加到双向链表时,会有更多的链接,因此需要关注更多的情况。

(1) The list is empty. (1) 列表为空。 Add the incoming node to the head of the list.将传入节点添加到列表的头部。 The next pointer and the before pointer point to nothing (null)下一个指针和前一个指针指向空(空)

在此处输入图像描述

(2) There is one node in the list, and we append the incoming node to the front of the list. (2)链表中有一个节点,我们把append传入的节点放到链表的最前面。 Here we append the incoming node's next to the head of the list, and the node that was already there points the before pointer back at the incoming node.在这里,我们 append 传入节点的 next 到列表的头部,并且已经在那里的节点将 before 指针指向传入节点。

在此处输入图像描述

(3) Okay, now assume the incoming node goes in between two nodes. (3) 好的,现在假设传入节点进入两个节点之间。 This is more complicated.这更复杂。 Attach the incoming node's before to the previous node, and then attach the incoming node's next to the node that's next in the list.将传入节点的 before 附加到前一个节点,然后将传入节点的 next 附加到列表中的下一个节点。 Now, relink the previous node's next to point at the incoming node, and then do the same with the node in front, but this time with the before link.现在,重新链接前一个节点的 next 以指向传入节点,然后对前面的节点执行相同的操作,但这次使用 before 链接。 在此处输入图像描述

(4) Okay, one last case to be concerned about. (4) 好的,最后一个需要关注的案例。 Append the incoming node at the end of the list. Append 传入节点位于列表末尾。 This is similar to appending to the front of the list, but now, we use the incoming node's back pointer to point at the previous node, and like all the other cases, don't forget to update the link at the previous node.这类似于追加到列表的前面,但是现在,我们使用传入节点的反向指针指向前一个节点,并且像所有其他情况一样,不要忘记更新前一个节点的链接。 In other words, point the previous node's next pointer at the incoming node.换句话说,将前一个节点的下一个指针指向传入节点。

在此处输入图像描述

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

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