Deletion of item from double linked list st known position takes O(1) complexity while deletion at some specified position takes O(n) time complexity.
In deletion we basically removes the pointer of that item. First of all check the reference node value that would be equal to after value. Then set the link of ref node to temp node so that middle node could be remove.
public void deleteAfterPos(T after){
Node<T> tempNode = start;
Node<T> refNode = null;
while(true){
if (tempNode == null){
break;
}else{
if (tempNode.compareTo(after) == 0){
refNode = tempNode;
break;
}
}
tempNode = tempNode.getNextReference();
}
if (refNode != null){
tempNode = refNode.getNextReference().getNextReference();
refNode.setNextReference(tempNode);
tempNode.setPrevReference(refNode);
}
}
0 Comment(s)