public void addLast(Item item) { if (item == null) { throw new java.lang.NullPointerException("Cannot add a null item!"); } Node oldLast = last; last = new Node(); last.item = item; last.next = null; if (isEmpty()) { last.pre = null; first = last; } else { oldLast.next = last; last.pre = oldLast; } N++; }
public void addFirst(Item item) { if (item == null) { throw new java.lang.NullPointerException("Cannot add a null item!"); } Node oldFirst = first; first = new Node(); first.item = item; first.pre = null; if (last == null) { // condition must be this. Cannot be // "if(isEmpty())"!!! because it can't be empty now last = first; } else { first.next = oldFirst; oldFirst.pre = first; } N++; }