簡體   English   中英

鏈表 - 如何制作一個遞歸添加方法,添加和“排序”?

[英]Linked List - How to make a recursive Adding method that add and “ sort ”?

您好,我正在創建自己的鏈接列表,但沒有實現 java.util.linkedlist

我想創建一個遞歸添加方法:

  • 應該添加一個級別介於較低級別和較高級別的口袋妖怪之間的口袋妖怪像這樣:

Bulbasaur(5) -> Squirtle(15) -> Charmander(20)

然后 add(Pigeon) 其級別為 6 所以:

Bulbasaur(5) -> Pigeon(6) -> Squirtle(15) -> Charmander(20)

添加鴿子是我掙扎的部分

到目前為止,我已經設法對它們進行了排序,因為我一直在將它們從最小到最大添加:

d1.addPokemon(p1); // 等級 5

d1.addPokemon(p2); // 等級 15

d1.addPokemon(p3); //20級

d1.addPokemon(p4); // 級別 6 - 不添加,我的方法有問題我不知道要更改什么

謝謝

        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 ! :(

            }

        }

更改addPokemon方法Pokeball這樣的類。

 public void addPokemon(Pokeball current, Pokemon pok) {
     // 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);
   }

}

在 Trainer 類更改addPokemon方法;

 public void addPokemon(Pokemon pok) {
        if (this.head != null) {
            if(pok.getLevel()<this.head.pok.getLevel()){
                Pokeball newPokeball = new Pokeball(pok);
                newPokeball.next = this.head;
                this.head = newPokeball;
                return;
            }
            this.head.addPokemon(this.head, pok);
        } else {
            this.head = new Pokeball(pok);
        }
    }

好的,讓我們簡單地遍歷列表,每次檢查下一個節點是否超過一個新節點。 最直接的方法是:

public void addPokemon(Pokemon pokemon) {
    if (head == null) {
        head = new Pokeball(pokemon, null);
        return; 
    }
    Pokeball current = head;
    while(current.getPokemon().getLevel() < pokemon.getLevel()) { 
        if(current.getNext() == null) {
            current.getNext() == new Pokeball(pokemon, null);
            return;
        } else {
            if (current.getNext().getPokemon().getLevel() > pokemon.getLevel()) {
                current.setNext(new Pokeball(pokemon, current.getNext()))
                return;
        }
    }
    return;

不要忘記添加所需的 getter 和 setter。 在這里閱讀為什么這是好的做法。 還有很多其他的方法來實現這個,不要忘記你可以實現IteratorComparable然后使用一些 java 工具,比如sort()compareTo() ,但這些方法更復雜,這里不需要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM