public class NLinkedListNode { private E value; private NLinkedListNode previous = null, next = null; public NLinkedListNode(E value){ this.setValue(value); } public E getValue(){ return this.value; } public void setValue(E value){ this.value = value; } public NLinkedListNode getNext(){ return this.next; } public void setNext(NLinkedListNode next){ if(this.next != null && this.next.previous == this) this.next.previous = null; this.next = next; if(this.next != null) this.next.previous = this; } public NLinkedListNode getPrevious(){ return this.previous; } public void setPrevious(NLinkedListNode previous){ if(this.previous != null && this.previous.next == this) this.previous.next = null; this.previous = previous; if(this.previous != null) this.previous.next = this; } /** * Adds this node after another node * @param node the node this node should be after */ public void addAfter(NLinkedListNode node){ assert node != null : "Cannot add a node after null"; NLinkedListNode next = node.next; this.setPrevious(node); this.setNext(next); } /** * Adds this node before another node * @param node the node this node should be before */ public void addBefore(NLinkedListNode node){ assert node != null : "Cannot add a node before null"; NLinkedListNode prev = node.previous; this.setNext(node); this.setPrevious(prev); } public void remove(){ if(this.previous != null) this.previous.setNext(this.next); if(this.next != null) this.next.setPrevious(this.previous); } }