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(); } } }
this.node2.getPrevious(), this.node1 ); assertSame( "Node2 next should be node3", this.node2.getNext(), this.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.node3 ); 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() );
public class LinkedList { private LinkedListNode head; public boolean find(Object data) { LinkedListNode temp = head; while(temp!= null) // check if you have reached the tail { if(data.equals(temp.getData())) { return true; } temp = temp.getNext(); // move to the next node } // end of while loop return false; } // end of find method }
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 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()
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 T next() { if ( this.current == null ) { return null; } final T node = this.current; this.current = this.current.getNext(); return node; }
/** * 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; }
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()
/** * 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; }
/** * 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++; }
/** * 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 ); } }
LinkedListNode rabbit = head; LinkedListNode tortoise = head; while(rabbit != null) { //repeat until the rabit reaches the end of the list for(int i = 0; rabbit != null && i < 2; i++) { rabbit=rabbit.getNext(); //let the rabbit hop the first and second time if(rabbit != null) { tortoise=tortoise.getNext(); //let the tortoise hop the first and second time } } if(rabbit != null) { rabbit=rabbit.getNext(); //let the rabbit hop a third time } } return tortoise; //if reached the end, we return where the tortoise ended
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 ); }
public int hashCode() { final int PRIME = 31; int result = 1; for ( LinkedListNode node = this.firstNode; node != null; node = node.getNext() ) { result = PRIME * result + node.hashCode(); } return result; }
this.list.add( this.node1 ); assertNull( "Node1 previous should be null", this.node1.getPrevious() ); assertNull( "Node1 next should be null", this.node1.getNext() ); assertSame( "First node should be node1", this.list.getFirst(), this.node1.getNext(), this.node2 ); assertSame( "node2 previous should be node1", this.node2.getPrevious(), this.node1 ); assertSame( "First node should be node1", this.node2.getNext(), this.node3 ); assertSame( "node3 previous should be node2", this.node3.getPrevious(), this.node2 ); assertEquals( "LinkedList should have 3 nodes",
public Object next() { this.currentNode = this.nextNode; if ( this.currentNode != null ) { this.nextNode = this.currentNode.getNext(); } else { throw new NoSuchElementException( "No more elements to return" ); } return this.currentNode; }
public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(firstNode); out.writeObject(lastNode); out.writeInt(size); out.writeObject(iterator); if ( firstNode == lastNode ) { // no other nodes return; } for (LinkedListNode node = firstNode; node != null; node = node.getNext()) { out.writeObject(node.getNext()); } } /**
public static void main(String[] args) { LinkedList list = new LinkedList(); list.insertFront(1); list.insertFront(2); list.insertFront(3); System.out.println(list.toString()); } String toString() { String result = ""; LinkedListNode current = head; while(current.getNext() != null){ current = current.getNext(); result += current.data + ", "; } return "List: " + result; }
Optional<LinkedListNode> recur( LinkedListNode currentNode, int nFromLast, Queue<LinkedListNode> latestNodes) { if (currentNode == null) { // went past the last node if (latestNodes.size() > nFromLast) return Optional.of(latestNodes.remove()); else // the queue is too short return Optional.empty(); } else { if (latestNodes.size() == (nFromLast + 1)) latestNodes.remove(); latestNodes.add(currentNode); return recur(currentNode.getNext(), nFromLast, latestNodes); } }