impl: cll get before & after
This commit is contained in:
@@ -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();
|
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) {
|
if (nextNode == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentNode.getItem().equals(item)) {
|
return traverseFind(nextNode, item, position);
|
||||||
return currentNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
return traverseFind(nextNode, item);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -62,19 +100,19 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T getAfter(T reference) {
|
public T getAfter(T reference) {
|
||||||
// TODO Auto-generated method stub
|
CustomLinkedListNode<T> node = traverseFind(head, reference, ItemPosition.AFTER);
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'getAfter'");
|
return node == null ? null : node.getItem();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T getBefore(T reference) {
|
public T getBefore(T reference) {
|
||||||
// TODO Auto-generated method stub
|
CustomLinkedListNode<T> node = traverseFind(head, reference, ItemPosition.BEFORE);
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'getBefore'");
|
return node == null ? null : node.getItem();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(T item) {
|
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) {
|
private int recursiveSizeCalc(CustomLinkedListNode<T> node, int count) {
|
||||||
|
|||||||
Reference in New Issue
Block a user