Hello i'm creating my own my linked list without implementing java.util.linkedlist
I want to create a recursive adding method that :
- should add a Pokemon whose level is between a pokemon who is lower level and higher level Something like this :
Bulbasaur(5) -> Squirtle(15) -> Charmander(20)
then add(Pigeon) whose level is 6 so :
Bulbasaur(5) -> Pigeon(6) -> Squirtle(15) -> Charmander(20)
Adding pigeon is the part where I'm struggling
So far, i've managed to sort them because I've been adding them from the smallest to the biggest :
d1.addPokemon(p1); // level 5
d1.addPokemon(p2); // level 15
d1.addPokemon(p3); //level 20
d1.addPokemon(p4); // level 6 - Not adding, there is something wrong with my method I don't know what to change
Thank you
        public class Trainer{
            public final String name;
            private Pokeball head;
            public Trainer(String name) {
                this.name = name;
            }
            public void display() {
                System.out.print(this.name + " : ");
                this.head.display();
            }
            public void addPokemon(Pokemon pok) {
                if (this.head != null) {
                    this.head.addPokemon(this.head, pok);
                } else {
                    this.head = new Pokeball(pok);
                }
            }
        }
        public class Pokeball {
            private Pokemon pok;
            private Pokeball next;
            public Pokeball(Pokemon pok) {
                this.pok = pok;
            }
  public Pokeball addPokemon(Pokeball current, Pokemon pok) {
        if (current == null) {
            return null;
        }
        if (current.pok.getLevel() > pok.getLevel()) {
            Pokeball newPokeball = new Pokeball(pok);
            newPokeball.next = current;
            return newPokeball;
        }
        // if next is null add it to next
        if (current.next == null) {
            current.next = new Pokeball(pok);
        }
        // if next is not null and value is between two sequences add it between
        else if (pok.getLevel() > current.pok.getLevel() && pok.getLevel() <= current.next.pok.getLevel()) {
            Pokeball newPokeball = new Pokeball(pok);
            newPokeball.next = current.next;
            current.next = newPokeball;
        }
        // If value is not between call recursion again
        else {
            addPokemon(current.next, pok);
        }
        return current;
    }
        public class Pokemon {
            private String name;
            private int level;
            public Pokemon(String name, int level) {
                this.name = name;
                this.level = level;
            }
            public void display() {
                System.out.println();
                System.out.print(this.name + " : " + this.level);
            }
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public int getLevel() {
                return level;
            }
            public void setLevel(int level) {
                this.level = level;
            }
        }
        public class test {
            public static void main(String[] args) {
                Pokemon p1 = new Pokemon("Bulbasaur", 5);
                Pokemon p2 = new Pokemon("Squirtle", 15);
                Pokemon p3 = new Pokemon("Charmander", 20);
                Pokemon p4 = new Pokemon("Pigeon", 6);
                Trainer t1 = new Trainer("Pierre");
                t1.addPokemon(p1);
                t1.addPokemon(p2);
                t1.addPokemon(p3);
                t1.addPokemon(p4);
                t1.display();
        // prints :
        Pierre : 
        Bulbasaur : 5 
        Squirtle : 15 
        Charmander : 20 
    // But pigeon is not here ! :(
            }
        }
 
     
    