リストから特定の情報をすべて見つけて削除します。(リストを 1 回だけトラバースします。)
リストの最初の番号を削除することができます。つまり、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;
}
}
}
}
}