简体   繁体   English

双链列表的加法

[英]Addback method for a doubly linked list

I realize this should be a very easy method to implement and I have no idea why this code is not working. 我意识到这应该是一种非常容易实现的方法,我也不知道为什么这段代码无法正常工作。

I get a NullPointerException at tail.next = node . 我在tail.next = node收到NullPointerException If it is not commented out, it does not pass the tester. 如果未将其注释掉,则它不会通过测试仪。

public void addBack (int x)
{
    IntegerNode node = new IntegerNode(x,null);
    if (head == null)
        head = node;
    else{
        node.prev = tail;
        //tail.next = node;
        tail = node;

    }

    count++;
}

When the list is empty, set your head and tail to the new element. 当列表为空时,将头和尾设置为新元素。 Should be ok after that, though more null checks are rarely a bad idea. 此后应该可以,尽管更多的空检查很少是一个坏主意。

if (head == null) {
    head = node;
    tail = node;
} else {
    node.prev = tail;
    tail.next = null; // Did you really mean tail.next = node?  This looks more likely..
    tail = node;
}

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

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