【问题标题】:How do I avoid unwanted recursion in my custom LinkedList implementation?如何在我的自定义 LinkedList 实现中避免不必要的递归?
【发布时间】:2014-10-16 15:39:47
【问题描述】:

我正在尝试在列表末尾插入一个新节点,但它一直在递归。

我做错了什么?

public class Main {

    public static void main(String[] args) {
        run();
    }

    private static void run() {
        LinkedList list = new LinkedList();
        list.add("abc");
        list.add("def");
        list.add("ghi");
        list.add("jkl");
    }
}

add 方法首先检查列表是否为空。

如果是这样,它会创建一个头节点。

否则,它会尝试找到列表的末尾并在那里插入新节点。

public class LinkedList<T> {

    Element head;
    Element terminator = new Element("TERMINATOR", true);

    public void add(T e) {
        Element node = new Element(e);
        if(head==null){
            head = node;
            head.setNext(terminator);
        }
        else {
            Element end = getEnd2();
            end.setNext(node);
        }
    }

    public Element getEnd2() {
        Element tmp;
        while((tmp = head.getNext())!=null){
            System.out.println("tmp:" + tmp.getValue());
        }
        return tmp;
    }

    public Element getEnd(){
        Element node = head;
        while(node!=null){
            System.out.println("node:" + node.getValue());
            node = head.getNext();
        }
        return node;
    }

    public Element getHead(){
        return head;
    }
}

public class Element<T>{
    T value;
    Element<T> next;

    boolean terminator;

    Element(T value){
        this.value = value;
    }

    Element(T value, boolean terminator){
        this.value = value;
        this.terminator = terminator;
    }

    public void setNext(Element<T> next) {
        this.next = next;
    }

    public Element getNext(){
        return next;
    }

    public T getValue(){
        return value;
    }

    public boolean isTerminator() {
        return terminator;
    }

    public void setTerminator(boolean terminator) {
        this.terminator = terminator;
    }

}

【问题讨论】:

  • 不断递归是什么意思?代码的结果是什么?期望的结果是什么?

标签: java algorithm recursion data-structures linked-list


【解决方案1】:

你的循环是无限的:

public Element getEnd(){
    Element node = head;
    while(node!=null){
        System.out.println("node:" + node.getValue());
        node = head.getNext(); // head.getNext() always returns the same value
    }
    return node;
}

如果你把它改成node = node.getNext(),你的方法只会返回null。

如果您想要最后一个非空节点,请将其更改为:

public Element getEnd(){
    Element node = head;
    while(node.getNext()!=null){
        System.out.println("node:" + node.getValue());
        node = node.getNext();
    }
    return node;
}

getEnd2() 也有同样的无限循环问题,但如果不使其与getEnd() 完全相同,则无法修复它,因为您无法对node 进行赋值和空值检查语句(因为您只想在验证您没有将 null 分配给 node 后进行分配)。

【讨论】:

    【解决方案2】:

    不是递归,是infitie循环就行了:

    while((tmp = head.getNext())!=null)
    

    条件永远不会改变,所以如果head.getNext() != null,这将永远循环。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-06
      • 2010-12-25
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      相关资源
      最近更新 更多