impl: cll to array list

This commit is contained in:
Václav Přibík
2025-10-24 13:05:35 +02:00
parent e28e179e2b
commit 145682e44f

View File

@@ -1,5 +1,6 @@
package appointmentplanner.customlist; package appointmentplanner.customlist;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -122,6 +123,7 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
} }
CustomLinkedListNode<T> nodeBefore = traverseFind(head, reference, ItemPosition.BEFORE); CustomLinkedListNode<T> nodeBefore = traverseFind(head, reference, ItemPosition.BEFORE);
CustomLinkedListNode<T> nodeToInsert = new CustomLinkedListNode<>(nodeBefore.getNext(), item); CustomLinkedListNode<T> nodeToInsert = new CustomLinkedListNode<>(nodeBefore.getNext(), item);
nodeBefore.setNext(nodeToInsert); nodeBefore.setNext(nodeToInsert);
} }
@@ -204,4 +206,18 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
return items; return items;
} }
private ArrayList<T> mapToArrayList(CustomLinkedListNode<T> node, ArrayList<T> list) {
if (node == null) {
return list;
}
mapToArrayList(node.getNext(), list);
list.add(node.getItem());
return list;
}
@Override
public ArrayList<T> toArrayList() {
return mapToArrayList(head, new ArrayList<>());
}
} }