简体   繁体   English

java-Doubly Linked List逻辑

[英]java-Doubly Linked List logic

I am trying to understand a java implementation for doubly linked list. 我试图理解双链表的java实现。 I have the following code: 我有以下代码:

public class DLLNode{
    //define variables
    public int info;
    public DLLNode next;
    public DLLNode prev;


    //Passing constructors
    public DLLNode(int i){
        info = i;
        next = prev = null;
    }

    public DLLNode(int i, DLLNode n, DLLNode p){
        info = i;
        next = n;
        prev = p;
    }
}

And the following: 以下内容:

public class DLL {
    DLLNode head;
    DLLNode tail;

    public DLL(){
        head = tail = null;
    }

    //Check whether list is empty or not
    public boolean isEmpty(){
        return head == null;
    }

//Insert element to head
    public void insertHead(int n){
        if(isEmpty()){
            head = tail = new DLLNode(n);
        }
        else{
            head = new DLLNode(n, null, head);
            head.next.prev = head;
        }
    }

Only the insertHead() method is show here for clarity. 为清楚起见,此处仅显示insertHead()方法。

Now I understand that if someone runs insertHead(10) in the main method, if the list is empty; 现在我明白,如果有人在main方法中运行insertHead(10),如果列表为空; a new object forms and both head and tail reference variables points to that object. 形成一个新对象,头部和尾部引用变量都指向该对象。

What I don't understand is if list is NOT empty; 我不明白的是,如果列表不是空的; the code piece is very confusing. 代码片非常混乱。

head = new DLLNode(n, null, head);
head.next.prev = head; //really confusing, what does this mean??

1)What I understand is n=10, null and head are passed to constructor: public DLLNode(int i, DLLNode n, DLLNode p).Then the assignment info=10, next=null and prev=head occurs. 1)我理解的是n = 10,null和head被传递给构造函数:public DLLNode(int i,DLLNode n,DLLNode p)。然后赋值info = 10,next = null和prev = head。 One problem is that, if at least one item is available on the list, and I add another item to HEAD position, shouldn't "next" point to the previous head, while "prev" point to null?? 一个问题是,如果列表中至少有一个项目可用,并且我将另一个项目添加到HEAD位置,则不应该“下一个”指向前一个头,而“prev”指向null? Faulty code perhaps?? 错误的代码也许??

2)What does the code 2)代码是什么

head.next.prev = head;

mean?? 意思?? And why is it necessary?? 为什么有必要? I really don't get that logic...:( 我真的没有那个逻辑...... :(

Any help would be appreciated.. 任何帮助,将不胜感激..

This implementation is wrong. 这种实现是错误的。 The insertHead would throw a NullPointerException if called more than once: 如果insertHead调用, insertHead将抛出NullPointerException

 head = new DLLNode(n, null, head);
 head.next.prev = head;  // head.next is null because of the above call

Instead, the implementation of the insert should be: 相反,插入的实现应该是:

public void insertHead(int n) {
    if (isEmpty()) {
        head = tail = new DLLNode(n);
    } else {
        head = new DLLNode(n, head, null);
        head.next.prev = head;
    }
}

Inserting the node at head is a two-step action: 在头部插入节点分为两步:

  1. Create the node, set its next pointer to point to the current head and assign it to be the new head of the list. 创建节点,将其下一个指针设置为指向当前头并将其指定为列表的新头。
  2. Set the previous pointer of the old head to the new head. 将旧头的先前指针设置为新头。 That's what the statement head.next.prev = head does. 这就是head.next.prev = head的声明。

Yes you are right, but the code is exactly doing what you say (just one mistake), maybe if we use brackets it becomes clearer: 是的,你是对的,但代码正是你所说的(只是一个错误),也许如果我们使用括号,它会变得更清晰:

(head.next).prev = head;

We assign the node we just created to the next node. 我们将刚刚创建的节点分配给下一个节点。 Or in other words: we update the prev reference of the old head to the new head which is necessairy because 换句话说:我们将旧头部的上级参考更新为新头部,这是必要的,因为

head = new DLLNode(n, null, head);

creates a new head where previous points to null and next is the old head, but the reference of previous of the old head is still null . 创建一个新头,其中前一个指向null ,next是旧头,但旧头的前一个引用仍然为null

You have the elements in the constructor in the wrong order (or in the call for creating the new head). 构造函数中的元素的顺序错误(或者在创建新头的调用中)。

public DLLNode(int i, DLLNode p, DLLNode n) {

And it works. 它有效。

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

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