//This piece of code is inspired by the one of Apache whose licence is in the file DoublyLinkedList.java in the same directory
package related;
/**
* Basic doubly linked list node interface.
*/
public class NodeDLL {
public NodeDLL next;
public NodeDLL prev;
public NodeDLL getNext() { return next; }
public NodeDLL getPrev() { return prev; }
public void setNext(NodeDLL newNext) { next = newNext; }
public void setPrev(NodeDLL newPrev) { prev = newPrev; }
/**
* Unlink this node from it's current list...
*/
public void unlink()
{
if (getNext() != null)
getNext().setPrev(getPrev());
if (getPrev() != null)
getPrev().setNext(getNext());
setNext(null);
setPrev(null);
}
/**
* Link this node in, infront of nde (unlinks it's self
* before hand if needed).
* @param nde the node to link in before.
*/
public void insertBefore(NodeDLL nde)
{
// Already here...
if (this == nde) return;
if (getPrev() != null)
unlink();
// Actually insert this node...
if (nde == null) {
// empty lst...
setNext(this);
setPrev(this);
} else {
setNext(nde);
setPrev(nde.getPrev());
nde.setPrev(this);
if (getPrev() != null)
getPrev().setNext(this);
}
}
}
|