impl: cll get before & after

This commit is contained in:
Václav Přibík
2025-10-17 12:48:27 +02:00
parent 2dc0dd706c
commit 595f2f2344

View File

@@ -27,19 +27,57 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
}
}
private CustomLinkedListNode<T> traverseFind(CustomLinkedListNode<T> currentNode, T item) {
private static enum ItemPosition {
AFTER, BEFORE, CURRENT
}
private CustomLinkedListNode<T> traverseFind(CustomLinkedListNode<T> currentNode, T item, ItemPosition position) {
CustomLinkedListNode<T> 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<T> implements CustomLinkedList<T> {
@Override
public T getAfter(T reference) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getAfter'");
CustomLinkedListNode<T> 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<T> 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<T> node, int count) {