从列表中查找并删除所有出现的给定信息。(仅遍历列表一次。)
我可以删除列表中的第一个数字,即如果我选择 1。
并且列表是“1 3 4 6 7” 1 将被删除,并且计数将减少到 4,就像它应该的那样。
public void deleteAll(T deleteItem) {
LinkedListNode<T> current ; // variable to traverse the list
LinkedListNode<T> trailCurrent ; // variable just before current
// boolean found;
if (first == null) // Case 1; the list is empty
System.err.println("Cannot delete from an empty " + "list.");
else {
if (first.info.equals(deleteItem)) // Case 2
{
first = first.link;
if (first == null) // the list had only one node
last = null;
count--;
}
else{
trailCurrent = first;
current = first.link;
while(current != null){
if(current.info.equals(deleteItem)){
trailCurrent = current.link;
count--;
}
else
{
trailCurrent = current;
current = current.link;
}
}
}
}
}