public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { firstNode = (T)in.readObject(); lastNode = (T)in.readObject(); size = in.readInt(); iterator = (LinkedListIterator)in.readObject(); LinkedListNode current = firstNode; LinkedListNode previous = null; while ( current != lastNode) { LinkedListNode next = (LinkedListNode) in.readObject(); current.setPrevious(previous); current.setNext(next); previous = current; current = next; } // current equals last Node, so set previous (this avoids the null writting in stream current.setPrevious( previous ); }
/** * Add a <code>LinkedListNode</code> to the list. If the <code>LinkedList</code> is empty then the first and * last nodes are set to the added node. * * @param node * The <code>LinkedListNode</code> to be added */ public void add(final T node) { if ( this.firstNode == null ) { this.firstNode = node; this.lastNode = node; } else { this.lastNode.setNext( node ); node.setPrevious( this.lastNode ); this.lastNode = node; } this.size++; }
/** * Remove the first node from the list. The next node then becomes the first 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 T removeFirst() { if ( this.firstNode == null ) { return null; } final T node = this.firstNode; this.firstNode = node.getNext(); node.setNext( null ); if ( this.firstNode != null ) { this.firstNode.setPrevious( null ); } else { this.lastNode = null; } this.size--; return node; }
/** * 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; }
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++; }
/** * 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 ); } }