问题描述
我在编码方面有点新,所以我想我的问题应该是非常基本的哈哈 基本上,我在 Java 程序中使用循环双向链表。 我已经成功地实现了插入、打印和其他方法,但我在使用两种方法(搜索和删除)时遇到了一些问题。
我有一个使用插入代码成功工作的列表类:
public class ListaDC {
private NodoLCD head;
private NodoLCD bottom;
public void inserta(Planetas p) {
if (head == null) {
head = new NodoLCD(p);
bottom = head;
bottom.setNext(head);
head.setBack(bottom);
} else if (p.getId() > head.getDato().getId()) {
NodoLCD aux = new NodoLCD(p);
aux.setNext(head);
head = aux;
bottom.setNext(head);
head.setBack(bottom);
} else if (bottom.getDato().getId() >= p.getId()) {
bottom.setNext(new NodoLCD(p));
bottom = bottom.getNext();
bottom.setNext(head);
head.setBack(bottom);
} else {
NodoLCD aux = head;
while (aux.getNext().getDato().getId() > p.getId()) {
aux = aux.getNext();
}
NodoLCD temp = new NodoLCD(p);
temp.setNext(aux.getNext());
temp.setBack(aux);
aux.setNext(temp);
temp.getNext().setBack(temp);
}
}
这是我的 Node 类的快照:
private Planetas dato;
private NodoLCD next;
private NodoLCD back;
public NodoLCD(Planetas dato) {
this.dato = dato;
}
它有所有的 getter、setter 和 toString
我还有一个名为行星的类,其中包含一些变量,如名称、位置、类型
问题是两种方法都在同一部分失败(如果条件):
public boolean existe(int id) {
boolean esta = false;
if (head!= null) {
if ((id >= head.getDato().getId()) && (id <= last.getDato().getId())) {
NodoLCD aux = cabeza;
while (aux != bottom&& aux.getDato().getId() < id) {
aux = aux.getNext();
}
esta = (aux.getDato().getId() == id);
}
}
return esta;
}
这是我的搜索方法代码,它永远不会进入第一个 if,即使有与请求匹配的 ID。
我错过了什么吗?你们需要更多信息或代码吗?
提前感谢您的时间:) 我真的很感激
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)