this.node2.getPrevious(), this.node1 ); assertSame( "Node2 next should be node3", this.list.remove( this.node2 ); assertNull( "Node2 previous should be null", this.node2.getPrevious() ); assertNull( "Node2 next should be null", this.node2.getNext() ); this.node1.getPrevious() ); assertSame( "Node1 next should be node3", this.node1.getNext(), this.list.remove( this.node1 ); assertNull( "Node1 previous should be null", this.node1.getPrevious() ); assertNull( "Node1 next should be null", this.node1.getNext() ); this.node3.getPrevious() ); assertNull( "Node3 next should be null", this.node3.getNext() ); this.list.remove( this.node3 ); assertNull( "Node3 previous should be null", this.node3.getPrevious() ); assertNull( "Node3 next should be null", this.node3.getNext() );
this.list.add( this.node1 ); assertNull( "Node1 previous should be null", this.node1.getPrevious() ); assertNull( "Node1 next should be null", this.node1.getNext() ); this.node2 ); assertSame( "node2 previous should be node1", this.node2.getPrevious(), this.node1 ); assertSame( "First node should be node1", this.node3 ); assertSame( "node3 previous should be node2", this.node3.getPrevious(), this.node2 ); assertEquals( "LinkedList should have 3 nodes",
/** * Remove the last node from the list. The previous node then becomes the last node. If this is the last * node then both first and last node references are set to null. * * @return * The first <code>LinkedListNode</code>. */ public LinkedListNode removeLast() { if ( this.lastNode == null ) { return null; } final LinkedListNode node = this.lastNode; this.lastNode = (T) node.getPrevious(); node.setPrevious( null ); if ( this.lastNode != null ) { this.lastNode.setNext( null ); } else { this.firstNode = this.lastNode; } this.size--; return node; }
/** * Removes a <code>LinkedListNode</code> from the list. This works by attach the previous reference to the child reference. * When the node to be removed is the first node it calls <code>removeFirst()</code>. When the node to be removed is the last node * it calls <code>removeLast()</code>. * * @param node * The <code>LinkedListNode</code> to be removed. */ public void remove(final T node) { if ( this.firstNode == node ) { removeFirst(); } else if ( this.lastNode == node ) { removeLast(); } else { node.getPrevious().setNext( node.getNext() ); (node.getNext()).setPrevious( node.getPrevious() ); this.size--; node.setPrevious( null ); node.setNext( null ); } }
public void insertAfter(final T existingNode, final T newNode) { if ( newNode.getPrevious() != null || newNode.getNext() != null ) { //do nothing if this node is already inserted somewhere return; } if ( existingNode == null ) { if ( this.isEmpty() ) { this.firstNode = newNode; this.lastNode = newNode; } else { // if existing node is null, then insert it as a first node final LinkedListNode node = this.firstNode; node.setPrevious( newNode ); newNode.setNext( node ); this.firstNode = newNode; } } else if ( existingNode == this.lastNode ) { existingNode.setNext( newNode ); newNode.setPrevious( existingNode ); this.lastNode = newNode; } else { (existingNode.getNext()).setPrevious( newNode ); newNode.setNext( existingNode.getNext() ); existingNode.setNext( newNode ); newNode.setPrevious( existingNode ); } this.size++; }