Blebhebhe
Blebhebhe

Reputation: 93

How to display elements in a Linked list by recursion?

Hello this is my linked list without implementing java.util.linkedlist

I'd like to create a method that display recursively all of the elements in my linked list but I have no idea how to, my method doesn't have any parameters so I don't know how to get to the next value when calling the method itself

    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 { 
        // ....
    }


    }

Upvotes: 0

Views: 506

Answers (2)

cst1992
cst1992

Reputation: 3931

Usually this is accomplished using a private helper function that does have a parameter.

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

Upvotes: 1

SomeDude
SomeDude

Reputation: 14228

I assume this is for Pokeballclass, Why you would like to display using recursion ? why not iteration? Your Trainer class doesn't create a linked list, it has no 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;
    }
}

Recursive:

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

You may call this like:

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

Upvotes: 1

Related Questions