【问题标题】:How to display elements in a Linked list by recursion?如何通过递归显示链表中的元素?
【发布时间】:2018-02-23 20:23:01
【问题描述】:

你好,这是我没有实现 java.util.linkedlist 的链表

我想创建一个递归显示链表中所有元素的方法,但我不知道怎么做,我的方法没有任何参数,所以我不知道如何进入下一个调用方法本身时的值

    public class Pokemon {
        private String name;
        private String type;
        private int niveau;

        public Pokemon(String name, String type) {
            this.name = name;
            this.type = type;
            this.niveau = (int) (Math.random() * (1 * 1 - 100) + 100);
        }
        public void display() {
            System.out.println(this.name);
       }

    public class Trainer {

        public final String name;
        private Pokeball head;

        public Trainer(String name) {
            this.name = name;
        }

        public void addPokemon(Pokemon pok) {
            if (this.head != null) {
                this.head.addPokemon(pok);
            } else {
                this.head = new Pokeball(pok);
            }
        }

       public void display() {
            if (this.head == null)
        return;
    else {
        this.head.display();
    }


    }

    public class Pokeball {

        private Pokemon pok;
        private Pokeball next;

        public Pokeball(Pokemon pok) {
            this.pok = pok;
        }

        public Pokeball(Pokemon pok, Pokeball next) {
            this.pok = pok;
            this.next = next;
        }

        public void addPokemon(Pokemon pok) {
            Pokeball current = this;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new Pokeball(pok);
        }

public void display() {
    Pokeball current = this;
    if (current.next == null){
        return;
    } else { 
        // ....
    }


    }

【问题讨论】:

  • 正确的缩进对于我们能够轻松阅读和理解您的代码至关重要。请修复它。

标签: java recursion linked-list


【解决方案1】:

我假设这是针对Pokeballclass,为什么要使用递归显示?为什么不迭代?您的 Trainer 类没有创建链接列表,它没有 next

public void display() {
    Pokeball current = this; //Note that this won't change your 'this'
    while ( current != null ) {
        System.out.print( current.display() + "->" );
        current = current.next;
    }
}

递归:

private void display_recurse( Pokeball current ) {
    if ( current == null )
      return;
    System.out.print( current.display() + "->" );
    display_recurse( current.next);
}

你可以这样称呼:

public void display() {
   display_recurse( this );
}

【讨论】:

  • 这是我的作业,它告诉我使用递归我已经通过迭代完成了它
  • 谢谢,但我需要不使用参数,可以吗?
【解决方案2】:

通常这是使用具有参数的私有辅助函数来完成的。

public void display() {
    Pokeball current = this;
    display(current);
}

private void display(Pokeball toDisplay) {
    if(toDisplay == null) {
        return;
    } else {
        // code to display current here
        // ...
        display(toDisplay.next);
    }
}

【讨论】:

  • 你返回null ?而 OP 希望它打印一些东西?
  • 那又怎样?也可以使用void 来完成。返回类型仅用于检查何时停止。我也可以做toDisplay.next == null) 并获得相同的结果。这样做是为了更容易理解。我将更新以删除返回类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-05
  • 1970-01-01
  • 1970-01-01
  • 2022-06-28
  • 2021-05-29
  • 1970-01-01
相关资源
最近更新 更多