From 595f2f23447bc0dbdf379faa68f63a19030c64c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20P=C5=99ib=C3=ADk?= Date: Fri, 17 Oct 2025 12:48:27 +0200 Subject: [PATCH] impl: cll get before & after --- .../customlist/CustomLinkedListImpl.java | 60 +++++++++++++++---- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java b/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java index 1e82fd8..53a1e3f 100644 --- a/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java +++ b/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java @@ -27,19 +27,57 @@ public class CustomLinkedListImpl implements CustomLinkedList { } } - private CustomLinkedListNode traverseFind(CustomLinkedListNode currentNode, T item) { + private static enum ItemPosition { + AFTER, BEFORE, CURRENT + } + + private CustomLinkedListNode traverseFind(CustomLinkedListNode currentNode, T item, ItemPosition position) { CustomLinkedListNode nextNode = currentNode.getNext(); + switch (position) { + + case CURRENT: + + if (currentNode.getItem().equals(item)) { + return currentNode; + } + + break; + + case AFTER: + + if (nextNode == null) { + return null; + } + + if (nextNode.getItem().equals(item)) { + return currentNode; + } + + break; + + case BEFORE: + + if (nextNode == null) { + return null; + } + + if (currentNode.getItem().equals(item)) { + return nextNode; + } + + break; + + default: + return null; + } + if (nextNode == null) { return null; } - if (currentNode.getItem().equals(item)) { - return currentNode; - } - - return traverseFind(nextNode, item); + return traverseFind(nextNode, item, position); } @Override @@ -62,19 +100,19 @@ public class CustomLinkedListImpl implements CustomLinkedList { @Override public T getAfter(T reference) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getAfter'"); + CustomLinkedListNode node = traverseFind(head, reference, ItemPosition.AFTER); + return node == null ? null : node.getItem(); } @Override public T getBefore(T reference) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getBefore'"); + CustomLinkedListNode node = traverseFind(head, reference, ItemPosition.BEFORE); + return node == null ? null : node.getItem(); } @Override public boolean contains(T item) { - return traverseFind(head, item) != null ? true : false; + return traverseFind(head, item, ItemPosition.CURRENT) != null ? true : false; } private int recursiveSizeCalc(CustomLinkedListNode node, int count) {