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("b");
myList.add("c"); myList.add("c");
myList.add("d"); myList.add("d");
myList.add("e"); // myList.add("e");
myList.add("f"); // myList.add("f");
myList.delete("e"); myList.delete("a");
myList.delete("b");
myList.delete("c");
myList.delete("d");
myList.printALL(); myList.printALL();
} }
} }

View File

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