Delete works?

This commit is contained in:
Matti 2024-05-06 18:32:40 +02:00
parent ac503234b2
commit 7d1af4bd85
2 changed files with 24 additions and 8 deletions

View File

@ -14,10 +14,13 @@ public class Anwendung {
myList.add("b");
myList.add("c");
myList.add("d");
myList.add("e");
myList.add("f");
// myList.add("e");
// myList.add("f");
myList.delete("e");
myList.delete("a");
myList.delete("b");
myList.delete("c");
myList.delete("d");
myList.printALL();
}
}

View File

@ -33,6 +33,9 @@ public class LinkedList<T>{
}
public void printALL(){ // correct???
if (anchor==null){
return;
}
Cell<T> next = anchor.next;
System.out.println(anchor.data);
@ -64,22 +67,32 @@ public class LinkedList<T>{
}
public void delete(T value){
System.out.println("Removing " + '"' + value + '"');
if (anchor == null)return;
if (anchor.data == value){
anchor = anchor.next;
return;
}
if ((anchor.next != null) && (anchor.next.data == value)){
anchor.next = anchor.next.next;
}
var cell = anchor.next;
while (cell != null){
if (cell.next != null) {
while (cell != null && cell.next != null){
if (cell.next.data == value) {
if (cell.next.next != null) {
cell.next = cell.next.next;
return;
}
}
}
cell = cell.next;
if ((cell.next.next == null) && (cell.next.data == value)){
cell.next = null;
return;
}
cell = cell.next;
}
}
}