Java 无法理解头部节点 更多信息,请访问:

Java 无法理解头部节点 更多信息,请访问:,java,data-structures,Java,Data Structures,对于在addFirst(E元素)方法中使用java的DoublyInklist: private class Node { E element; Node next; Node prev; public Node(E element, Node next, Node prev) { this.element = element; this.next = next;

对于在
addFirst(E元素)
方法中使用java的DoublyInklist:

 private class Node {
        E element;
        Node next;
        Node prev;

        public Node(E element, Node next, Node prev) {
            this.element = element;
            this.next = next;
            this.prev = prev;
        }
    }
    /**
     * returns the size of the linked list
     * @return
     */
    public int size() { return size; }

    /**
     * return whether the list is empty or not
     * @return
     */
    public boolean isEmpty() { return size == 0; }

    /**
     * adds element at the starting of the linked list
     * @param element
     */
    public void addFirst(E element) {
        Node tmp = new Node(element, head, null);
        if(head != null ) {head.prev = tmp;}
        head = tmp;
        if(tail == null) { tail = tmp;}
        size++;
        System.out.println("adding: "+element);
    }
为什么
头=空
。由于我们没有在标题注释中添加任何元素,因此
默认值应为null。即检查第一条if语句时,
head==null

可能需要在LinkedList数据结构的头部插入一个非空的节点

例如,我可能有一个包含
{1,2}
的列表,我想在该列表的位置0插入值
0
。为此,我将在
节点上调用
addFirst()
,其中包含值
0
——当我这样做时,
head
不是
null

public void addFirst(E element) {
        Node tmp = new Node(element, head, null);
        if(head != null ) {head.prev = tmp;}
        head = tmp;
        if(tail == null) { tail = tmp;}
        size++;
        System.out.println("adding: "+element);
    }
编辑:
现在您在理解下面这行代码的工作原理时遇到了问题

public void addFirst(E element) {



        Node tmp = new Node(element, head, null);
        if(head != null ) {//if head is null don't go in this body
           head.prev = tmp;//that means there is no node so no prev.
}
// if there is head make new node to be prev of head which is doing in if block
        head = tmp;//it becomes the first node in that case
        if(tail == null) { tail = tmp;}
        size++;
        System.out.println("adding: "+element);
    }
这里有两种情况,一种是添加第一个节点,这是链表中唯一的节点;另一种是添加新的第一个节点,这是一个链表

让我们来看第二个场景,其中有一个链表
因此,对于一个新节点,您需要将其
next
指向
head
,作为上一个链接列表的头,并且由于
head
已经存在,因此您需要添加一个从
head
到其
prev
的链接,即
tmp

看到这张图了吗

现在,在第一个场景中,没有head节点已经存在,所以temp节点将指向null(head),这在一般情况下是这样做的。 所以这里

对于第一个场景,您实际上要传递
节点(element,null,null)

希望你现在明白为什么他们也会在第一个节点上通过头部

编辑2

Node tmp = new Node(element, head, null);
如果您尝试此操作,它将为第一个元素打印“它的非头”


所以不要混淆没有什么可混淆的。它工作得很好。

我有点困惑,当我第一次使用addFirst(E element)添加元素时,下一步是:Node tmp=new Node(element,head,null);然后检查if条件:if(head!=null){head.prev=tmp;},那么代码if(head!=null){head.prev=temp;}将为true,但我们仍然没有初始化节点head。如果(head!=null)为true,则仍然为null。如果(head!=null){head.prev=temp;},if条件第一次没有变为true当head为null时,您错误地认为它是真的,它只是绕过它,将head设置为temp.public void addFirst(E元素){if(head==null){System.out.println(“head为null”);}Node tmp=new Node(element,head,null);if(head!=null){System.out.println(“在tmp之后head不是null”);System.out.println(head.element);}if(head!=null){head.prev=tmp;}head=tmp;if(tail==null){tail=tmp;}size++;System.out.println(“添加:“+element”)}所以它将head.prev分配给temp?在其中使用一个print语句,然后告诉或只是将head和tail的声明位置初始化为null。我修改了该方法,以便检查它是否绕过,并将head分配给temp。它不会绕过if条件,正如上面代码中的if(head==null){System.out.println(“head是nullllllll”);}正在打印语句,如果(head!=null){System.out.println(“head在tmp之后不是nullllllll”);System.out.println(head.element);}也是正确的,它没有绕过任何内容。我有点困惑,当我第一次使用addFirst(E element)添加元素时下一步是:Node tmp=new Node(element,head,null);然后检查if条件:if(head!=null){head.prev=tmp;},那么代码if(head!=null){head.prev=temp;}将为true,但我们仍然没有初始化Node head。如果(head!=null)为true,则仍然为null。
Node tmp = new Node(element, head, null);
 public void addFirst(E element) {
        Node tmp = new Node(element, head, null);
        if(head != null ) {head.prev = tmp;
        System.out.println("its head");}
        if(head==null){System.out.println("its  not head");}
        head = tmp;
        if(tail == null) { tail = tmp;}
        size++;
        System.out.println("adding: "+element);
    }