杰尼龟-6ren">
gpt4 book ai didi

java - 链接列表 - 如何制作一个递归添加方法来添加 "sort "?

转载 作者:行者123 更新时间:2023-12-02 02:18:09 25 4
gpt4 key购买 nike

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

我想创建递归添加方法:

  • 应该添加一个等级介于低等级和高等级之间的宝可梦像这样的事情:

妙蛙种子(5) -> 杰尼龟(15) -> 小火龙(20)

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

妙蛙种子(5) -> 鸽子(6) -> 杰尼龟(15) -> 小火龙(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 ! :(

}

}

最佳答案

像这样更改 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);
}
}

关于java - 链接列表 - 如何制作一个递归添加方法来添加 "sort "?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48962887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com