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++;
 }
 public Item removeFirst() {
   if (isEmpty()) {
     throw new java.util.NoSuchElementException("Cannot remove an item from an empty deque!");
   }
   Item rm = first.item;
   first = first.next;
   first.pre = null;
   if (isEmpty()) {
     last = null;
   }
   N--;
   return rm;
 }