Receive a Position!
Here is the implementation of delete
from an earlier exercise:
public void delete(Node<T> target) {
Node<T> prevNode = target.prev;
Node<T> nextNode = target.next;
prevNode.next = nextNode;
nextNode.prev = prevNode;
}
Exercise Update delete
so that it receives a "Position" instead of a "Node".
// Pre: target's actual type is Node
public void delete(Position<T> target) {
// TODO Implement Me!
}
Solution
// Pre: target's "actual" type is Node
public void delete(Position<T> target) {
Node<T> targetNode = (Note<T>) target;
Node<T> prevNode = targetNode.prev;
Node<T> nextNode = targetNode.next;
prevNode.next = nextNode;
nextNode.prev = prevNode;
}
Notice we must explicitly downcast target
so we can access the next
and prev
reference variables.