Delete kinda works except for 1st and 2nd last element

This commit is contained in:
Matti 2024-05-06 18:05:13 +02:00
parent be8b24140e
commit ac503234b2
2 changed files with 18 additions and 14 deletions

View File

@ -10,16 +10,14 @@ public class Anwendung {
public static void main(String[] args){ public static void main(String[] args){
LinkedList myList = new LinkedList(); LinkedList myList = new LinkedList();
myList.add(5); myList.add("a");
myList.add("b");
myList.add("c");
myList.add("d");
myList.add("e"); myList.add("e");
myList.add(3.1415); myList.add("f");
myList.add(1);
myList.add(1);
myList.add(1);
myList.add(2);
myList.add(3);
myList.delete(1); myList.delete("e");
myList.printALL(); myList.printALL();
} }
} }

View File

@ -67,13 +67,19 @@ public class LinkedList<T>{
if (anchor.data == value){ if (anchor.data == value){
anchor = anchor.next; anchor = anchor.next;
} }
var next = anchor.next; var cell = anchor.next;
while (next != null){
if (next.next.data == value){ while (cell != null){
next.next = next.next.next;
return; if (cell.next != null) {
if (cell.next.data == value) {
if (cell.next.next != null) {
cell.next = cell.next.next;
return;
}
}
} }
next = next.next; cell = cell.next;
} }
} }
} }