代码之家  ›  专栏  ›  技术社区  ›  Blebhebhe

如何通过递归显示链表中的元素?

  •  0
  • Blebhebhe  · 技术社区  · 7 年前

    您好,这是我的链表,没有实现java。util。双链表

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

        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 { 
            // ....
        }
    
    
        }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   SomeDude    7 年前

    我想这是 Pokeball 类,为什么要使用递归显示?为什么不迭代?你的 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
  •  1
  •   cst1992    7 年前

    通常,这是通过使用具有参数的私有助手函数来实现的。

    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);
        }
    }