删除许多节点时出现问题。双向链表java删除
我可以将它们删除,如果我选择的节点是这样的:
但如果我这样做,我不能删除它们:
我的代码:
public boolean remove(ProductNode<E> data) {
if (isEmpty()) {
throw new NoSuchElementException();
}
for (ProductNode<E> current = this.head; current != null; current = current.next) {
ProductNode<E> pre = current.prev;
ProductNode<E> next = current.next;
if (data != null) {
if (current.data.equals(data.data)) {
if (pre == null) {
head = next;
current.next = null;
} else {
if (next != null) {
next.prev = pre;
}
}
if (next == null) {
pre.next = null;
current.prev = null;
tail = pre;
} else {
if (pre != null) {
pre.next = next;
}
}
}
}
}
size--;
return false;
}
搜索节点
public ProductNode<E> search(E data) {
for (ProductNode<E> current = this.head; current != null; current = current.next) {
if (current.data.equals(data)) {
return current;
}
}
return null;
}
删除
public void remove(E e) {
remove(search(e));
}
删除:
for(Tab_Product p : remove_list){
List_Products.list_products.remove(p);
}
你有没有尝试用调试器逐步完成代码?这可能会使得它更清楚发生了什么,以及何时(如果在删除相邻节点时发生问题,或者删除列表中的第一个节点时发生问题) – whrrgarbl