Compare commits

...

3 Commits

Author SHA1 Message Date
Matti
7d1af4bd85 Delete works? 2024-05-06 18:32:40 +02:00
Matti
ac503234b2 Delete kinda works except for 1st and 2nd last element 2024-05-06 18:05:13 +02:00
Matti
be8b24140e Delete kinda works 2024-05-06 17:46:02 +02:00
2 changed files with 47 additions and 1 deletions

View File

@ -8,6 +8,19 @@ package part1.aufg6;
*/ */
public class Anwendung { public class Anwendung {
public static void main(String[] args){ public static void main(String[] args){
System.out.println("Moin"); LinkedList myList = new LinkedList();
myList.add("a");
myList.add("b");
myList.add("c");
myList.add("d");
// myList.add("e");
// myList.add("f");
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??? 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);
@ -62,4 +65,34 @@ public class LinkedList<T>{
next = next.next; // must happen at end of loop next = next.next; // must happen at end of loop
} }
} }
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 && cell.next != null){
if (cell.next.data == value) {
if (cell.next.next != null) {
cell.next = cell.next.next;
return;
}
}
if ((cell.next.next == null) && (cell.next.data == value)){
cell.next = null;
return;
}
cell = cell.next;
}
}
} }