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++; }
private Node add(Node x, String key, int d) { if (x == null) x = new Node(); if (d == key.length()) { if (!x.isString) N++; x.isString = true; } else { char c = key.charAt(d); x.next[c - 65] = add(x.next[c - 65], key, d + 1); } return x; }
public Item removeLast() { if (isEmpty()) { throw new java.util.NoSuchElementException("Cannot remove an item from an empty deque!"); } Item rm = last.item; last = last.pre; last.next = null; if (isEmpty()) { first = null; } N--; return rm; }
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++; }