impl: cll insert before & after
This commit is contained in:
@@ -114,20 +114,28 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
|
||||
|
||||
@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<T> nodeBefore = traverseFind(head, reference, ItemPosition.BEFORE);
|
||||
CustomLinkedListNode<T> 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<T> referenceNode = traverseFind(head, reference, ItemPosition.CURRENT);
|
||||
CustomLinkedListNode<T> 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<T> node = traverseFind(head, reference, ItemPosition.BEFORE);
|
||||
return node == null ? null : node.getItem();
|
||||
@@ -136,7 +144,7 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
|
||||
@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<T> node = traverseFind(head, reference, ItemPosition.AFTER);
|
||||
return node == null ? null : node.getItem();
|
||||
|
||||
Reference in New Issue
Block a user