From 3330e31bcfe307becd5735edd13db608487d6879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20P=C5=99ib=C3=ADk?= Date: Sat, 18 Oct 2025 11:43:50 +0200 Subject: [PATCH] impl: cll insert before & after --- .../customlist/CustomLinkedListImpl.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java b/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java index ea46089..dc5a189 100644 --- a/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java +++ b/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java @@ -114,20 +114,28 @@ public class CustomLinkedListImpl implements CustomLinkedList { @Override public void insertAfter(T reference, T item) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'insertAfter'"); + + if (head.getItem().equals(reference)) { + add(item); + return; + } + + CustomLinkedListNode nodeBefore = traverseFind(head, reference, ItemPosition.BEFORE); + CustomLinkedListNode nodeToInsert = new CustomLinkedListNode<>(nodeBefore.getNext(), item); + nodeBefore.setNext(nodeToInsert); } @Override public void insertBefore(T reference, T item) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'insertBefore'"); + CustomLinkedListNode referenceNode = traverseFind(head, reference, ItemPosition.CURRENT); + CustomLinkedListNode nodeToInsert = new CustomLinkedListNode<>(referenceNode.getNext(), item); + referenceNode.setNext(nodeToInsert); } @Override public T getAfter(T reference) { - // head represents the last inserted item thus making enrire list in reverse + // head represents the last inserted item thus making entire list in reverse // order CustomLinkedListNode node = traverseFind(head, reference, ItemPosition.BEFORE); return node == null ? null : node.getItem(); @@ -136,7 +144,7 @@ public class CustomLinkedListImpl implements CustomLinkedList { @Override public T getBefore(T reference) { - // head represents the last inserted item thus making enrire list in reverse + // head represents the last inserted item thus making entire list in reverse // order CustomLinkedListNode node = traverseFind(head, reference, ItemPosition.AFTER); return node == null ? null : node.getItem();