简体   繁体   English

在Java中将双链列表数据结构添加到末尾

[英]Adding to the end of a Doubly-Linked List Data Structure in Java

I am working on my Computer Science studies and I am having some difficulty with adding a Node to the end of a doubly linked-list data structure. 我正在研究计算机科学,但是在将节点添加到双链表数据结构的末尾时遇到了一些困难。 I understand that the new node points to the tail and the tail points to it, thus I have this: 我知道新节点指向尾部,而尾部指向尾部,因此我有:

public boolean add(E element) 
    {
        // TODO: Implement this method
        LLNode<E> newNode = new LLNode<E> (element);
        if (element == null) {
            throw new NullPointerException("Element can not store a null reference!");

        } else {
            newNode.next = tail;
            newNode.prev = tail.prev;

            tail.prev = newNode;
            head.next = newNode;

        }
        size++;
        return true;
    }

The issue I'm having is trying to connect the head node (via head.next to the correct node). 我遇到的问题是尝试连接头节点(通过head.next连接到正确的节点)。

In my default constructor, I have the head.next node pointing to tail.prev. 在我的默认构造函数中,我的head.next节点指向tail.prev。 However in the add method, I could not figure out where head.next would point since each time you add a new node, head has to point to the first node in the LinkedList Data Structure. 但是在add方法中,由于每次添加一个新节点,head都必须指向LinkedList数据结构中的第一个节点,因此我无法弄清head.next的指向。 Here is the default constructor: 这是默认的构造函数:

public MyLinkedList() {
        // TODO: Implement this method
        size = 0;
        /*create two empty nodes at the head and the tail of the linked list*/
        head = new LLNode<E> (null);
        tail = new LLNode<E> (null);
        head.next = tail;
        tail.prev = head;
        head.prev = null; //Head is a sentinel node with no node prior to it
        tail.next = null; //tail is a sentinel node with no node after it
}

Please point me (no pun intended) to the right direction. 请指向我(不要双关语)指向正确的方向。 Thanks! 谢谢!

Draw on paper what you have to do. 在纸上画些你必须做的

Eg if you list current has two elements (A and B), you chain will be: 例如,如果列出当前元素有两个元素(A和B),则链接为:

HEAD <-> A <-> B <-> TAIL

To add a new element (C), your end result should be: 要添加新元素(C),最终结果应为:

HEAD <-> A <-> B <-> C <-> TAIL

which means the following updates: 这意味着以下更新:

  • C.prev = B or more precisely: C.prev = TAIL.prev C.prev = B或更准确地说: C.prev = TAIL.prev
  • B.next = C or more precisely: TAIL.prev.next = C B.next = C或更准确地说: TAIL.prev.next = C
  • C.next = TAIL
  • TAIL.prev = C

As you can see, HEAD is not involved in this, so your line head.next = newNode is wrong. 如您所见,HEAD 参与此操作,因此您的行head.next = newNode是错误的。

This should work. 这应该工作。

public boolean add(E element) 
    {
        LLNode<E> newNode = new LLNode<E> (element);
        if (element == null) {
            throw new NullPointerException("Element can not store a null reference!");
        } else {
            newNode.next = tail;       // set new.next to tail
            newNode.prev = tail.prev;  // set new.prev to prior last
            tail.prev.next = newNode;  // set prior last.next to new last
            tail.prev = newNode;       // set tail.prev to new last
            size++;
        }
        return true;
    }

I'm not sure if the check for null element is needed in an add function, although a null check may be needed elsewhere. 我不确定在添加函数中是否需要检查null元素,尽管在其他地方可能需要null检查。

I think you need to rethink how you handle the linked list... You start with a linked list Like this: 我认为您需要重新考虑如何处理链表...从链表开始,像这样:

head.next = tail
head.prev = null
tail.next = null
tail.prev = head

and when you add something to the end of the list, lets call it mid, you want your list to look like this: 当您在列表末尾添加内容时,将其命名为“ mid”,您希望列表看起来像这样:

head.next = mid (set by using tail.prev.next = mid)
head.prev = null
mid.next = tail (the new node is the end of the list)
mid.prev = head (previous tail.prev)
tail.next = null
tail.prev = mid (setting the new node to be the new end of the list)

So your mistake is that when you are adding a node, you shouldn't be using the head node explicitly, like you are doing. 因此,您的错误是,在添加节点时,您不应像正在做的那样显式地使用头节点。 All operations should be done in terms of the tail of the list. 所有操作都应按照列表的末尾进行。 I've it leave it up to you to turn this into code. 我把它变成代码由您自己决定。

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

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