From 53a09f6538fd1e25d5e08538a68f37f329770051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20P=C5=99ib=C3=ADk?= Date: Fri, 17 Oct 2025 13:01:47 +0200 Subject: [PATCH] impl: cll remove --- .../customlist/CustomLinkedListImpl.java | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java b/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java index 53a1e3f..5e49219 100644 --- a/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java +++ b/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java @@ -45,7 +45,7 @@ public class CustomLinkedListImpl implements CustomLinkedList { break; - case AFTER: + case BEFORE: if (nextNode == null) { return null; @@ -57,7 +57,7 @@ public class CustomLinkedListImpl implements CustomLinkedList { break; - case BEFORE: + case AFTER: if (nextNode == null) { return null; @@ -82,8 +82,27 @@ public class CustomLinkedListImpl implements CustomLinkedList { @Override public void remove(T item) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'remove'"); + + CustomLinkedListNode nodeToRemove = traverseFind(head, item, ItemPosition.CURRENT); + + if (nodeToRemove == null) { + return; + } + + if (nodeToRemove == head) { + head = head.getNext(); + return; + } + + CustomLinkedListNode beforeNode = traverseFind(head, item, ItemPosition.BEFORE); + + if (nodeToRemove.getNext() == null) { + beforeNode.setNext(null); + return; + } + + beforeNode.setNext(nodeToRemove.getNext()); + } @Override @@ -100,13 +119,19 @@ public class CustomLinkedListImpl implements CustomLinkedList { @Override public T getAfter(T reference) { - CustomLinkedListNode node = traverseFind(head, reference, ItemPosition.AFTER); + + // head represents the last inserted item thus making enrire list in reverse + // order + CustomLinkedListNode node = traverseFind(head, reference, ItemPosition.BEFORE); return node == null ? null : node.getItem(); } @Override public T getBefore(T reference) { - CustomLinkedListNode node = traverseFind(head, reference, ItemPosition.BEFORE); + + // head represents the last inserted item thus making enrire list in reverse + // order + CustomLinkedListNode node = traverseFind(head, reference, ItemPosition.AFTER); return node == null ? null : node.getItem(); }