/** * 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++; }
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 ); }
else { LinkedListNode pre = first; LinkedListNode node = first.getNextNode(); while(true){ if(node == null || node.getElement().time >= added.getElement().time) break; pre = node; node = node.getNext(); } pre.setNext(added); added.setNext(node); if(node == null) tail = added; }
public void insert(String data){ LinkedListNode newNode = new LinkedListNode(data); if(head == null){ head = newNode; }else{ LinkedListNode current = head; LinkedListNode prev; //This is missing some key edge cases, but it inserts properly in the general case. You'll have to add to it to handle things like running off the list, or this needing to be inserted before the head. while(current.getNext() != null){ if(current.compareTo(newNode)<0){ newNode.setNext(current); prev.setNext(newNode); break; } prev = current; current = current.getNext(); } } }
/** * 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++; }
public void delete(String deleteName) { LinkedListNode current = head; if(head==null){ System.out.println("Nothing to delete from the list! Insert data."); } // for head else if(current.getName().equals(deleteName)){ head=current.getNext(); } // for tail else if(findTail().getName().equals(deleteName)){ while(current.getNext().getNext()!=null ){ current = current.getNext(); } current.setNext(null); } else{ while(!current.getNext().getName().equals(deleteName)){ current=current.getNext(); } current.setNext(current.getNext().getNext()); } System.out.println(""); System.out.println(deleteName +" has been deleted from the list."); } // end of delete()
public void insertBefore(String later, String name){ if(head==null){ head = new LinkedListNode(name,null); } else if(head.getName()==later){ LinkedListNode newNode = makeNode(name); newNode.setNext(head); head=newNode; } else{ LinkedListNode current = head; while(current.getNext().getName()!=later){ current=current.getNext(); } LinkedListNode newNode = makeNode(name); // create the new node newNode.setNext(current.getNext()); current.setNext(newNode); } } // end of insertBefore()
/** * 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 ); } }