Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java LinkedList addToTail()不工作_Java_Algorithm_Linked List_Singly Linked List - Fatal编程技术网

Java LinkedList addToTail()不工作

Java LinkedList addToTail()不工作,java,algorithm,linked-list,singly-linked-list,Java,Algorithm,Linked List,Singly Linked List,我不明白为什么添加到这个LinkedList类的尾部不起作用,并且在输出中被忽略 下面是一个简单的节点类: public class IntNode { private int val; private IntNode next; public IntNode() { this.val = 0; IntNode next = null; } public IntNode(int val) { this.val = val; this.next = null;

我不明白为什么添加到这个LinkedList类的尾部不起作用,并且在输出中被忽略

下面是一个简单的节点类:

public class IntNode {

private int val;
private IntNode next;

public IntNode() {
    this.val = 0;
    IntNode next = null;
}

public IntNode(int val) {
    this.val = val;
    this.next = null;
}

public IntNode next() {
    return this.next;
}

public int getVal() {
    return this.val;
}

public void setNextNode(int val) {
    this.next = new IntNode(val);
}
public void setNextNode(IntNode a)
{
    this.next = new IntNode(a.getVal());
}

public void setVal(int val) {
    this.val = val;
}

public String toString() {
    StringBuffer buff = new StringBuffer();
    return toString(this, buff);
}

private String toString(IntNode node, StringBuffer buff) {
    if (node == null) {
        return buff.toString();
    }

    buff.append(node.val);
    if (node.next != null) {
        buff.append(", ");
    } else {
        buff.append(".");
    }

    return toString(node.next(), buff);
}

}
下面是它的链接列表:

public class LinkedList {

private IntNode header;
private IntNode trailer;
private int listSize;

public LinkedList()
{
    this.header = null;
    this.trailer = null;
    this.listSize = 0;
}
public LinkedList(IntNode a, IntNode b)
{
    this.header = a;
    this.trailer = b;
    this.header.setNextNode(this.trailer);
    this.listSize = 2;
}
public void addNode(IntNode a)
{
    this.trailer.setNextNode(a.getVal());
    this.trailer = this.trailer.next();
    this.listSize++;
}
public String toString()
{
    return this.header.toString();
}
public static void main(String args[])
{
    LinkedList lst = new LinkedList(new IntNode(1), new IntNode(2));
    lst.addNode(new IntNode(3));
    lst.addNode(new IntNode(4));
    System.out.println(lst.toString());
}

}
主要方法的输出是:1,2。
为什么加法方法不起作用?

在具有两个
IntNode
s的构造函数中,您的
没有指向您的
尾部。相反,它指向一个新的IntNode(traile.val)
。你应该改变

public void setNextNode(IntNode a)
{
    this.next = a;
}

您的问题是LinkedList构造函数将header设置为a,将trailer设置为b,但随后调用header.setNextNode(this.trailer)

IntNode的方法setNextNode()方法丢弃传递它的节点,而是创建一个与传递的节点具有相同值的节点

这意味着在LinkedList构造函数的末尾,您将头分配给a,该头的下一个节点值不是b,而是与b具有相同的值,而Traile设置为b

您应该更改setNextNode()方法,使其不丢弃交给它的节点,如下所示:

public void setNextNode(IntNode a) {
    this.next = a;
}

在LinkedList类的构造函数中,有代码
this.header.setNextNode(this.trailer)。这不会将头部的下一个节点设置为拖车,而是将头部的下一个节点设置为具有拖车值的另一个节点。当拖车的下一个节点被设置时,头部不会受到影响。

@SotiriosDelimanolis没有必要粗鲁。@nachokk我相信这没关系——这是一个递归方法。@mbroshi没有注意返回XD部分一般来说,当你想复制你的函数参数时,你需要重新考虑,当你只想引用它们时。这两者之间有很大的区别——除了造成错误(如答案中所述)和混乱(如导致错误)之外,不必要地分配和放弃对象代价高昂……此外,对于
toString()
之类的任务,您应该使用循环而不是递归。因为Java不会优化尾部递归调用,所以用长列表调用这种实现将导致可怕的堆栈溢出!