【问题标题】:Linked List - How to make a recursive Adding method that add and " sort "?链表 - 如何制作一个添加和“排序”的递归添加方法?
【发布时间】:2018-02-24 12:30:22
【问题描述】:

您好,我正在创建自己的链接列表,但没有实现 java.util.linkedlist

我想创建一个递归添加方法:

  • 应该添加一个级别介于低级别和高级别之间的口袋妖怪 像这样的东西:

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

然后添加等级为6的(鸽子):

Bulbasaur(5) -> 鸽子(6) -> Squirtle(15) -> Charmander(20)

添加鸽子是我苦苦挣扎的部分

到目前为止,我已经设法对它们进行了排序,因为我一直在将它们从最小到最大添加:

d1.addPokemon(p1); // 5级

d1.addPokemon(p2); // 15级

d1.addPokemon(p3); //20级

d1.addPokemon(p4); // level 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 ! :(

            }

        }

【问题讨论】:

  • 我没有看到 Dresseur 的 addPokemon()
  • 啊,是的,对不起,我已经编辑了我的变量是法语的,我忘了改这个
  • 以递归形式实现add 方法是否有任何义务?因为你总是有一个排序列表,当你想找到添加新口袋妖怪的正确位置时,你可以在没有递归的情况下非常清楚地做到这一点。
  • 我更新了答案,但是如果新的Pokeball是最小的级别,它是在Trainer类的addpokemon方法中添加的
  • @Blebhebhe 实际上它不符合 SO 规则,因为您要求使用递归方法,所以我的回答是错误的。

标签: java sorting linked-list


【解决方案1】:

像这样更改Pokeball 类中的 addPokemon 方法。

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

【讨论】:

  • 为什么不解释一下为什么要这样做?
  • 我认为第一个 pok beeing 的情况没有被涵盖,但我认为这是设计中的一个缺陷,因为您无法访问培训师的负责人
  • @Turo 我认为它包含在 Trainer 类 addPokemon 方法中。我直接在这里写了答案,没有测试它是否有效。
  • 谢谢它很好用!但正如图罗所说,如果鸽子是 4 级呢?我已经尝试过先解决这个问题但放弃了,我不确定我如何从 Pokeball 修改 Trainer 的值头
  • Trainer 类中不包含它,因此必须将其添加到那里或让 pokomon/pokoball 访问它的 trainer
【解决方案2】:

好的,让我们简单地遍历列表并每次检查下一个节点是否不仅仅是一个新节点。最直接的方法是:

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。阅读here 为什么这是一个好习惯。还有很多其他的实现方式,别忘了你可以实现IteratorComparable,然后使用一些java工具比如sort()或者compareTo(),但是这些方式更复杂,不需要在这里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多