/* Amortized O(1), worst-case O(n) */ public AmortizedPQueue<E> minus() { if (size() == 0) { return this; } int fsize = front.size(); if (fsize == 0) { // If there's nothing on front, dump back onto front // (as stacks, this goes in reverse like we want) // and take one off. return new AmortizedPQueue<E>(Empty.<E>stack().plusAll(back), Empty.<E>stack()).minus(); } else if (fsize == 1) { // If there's one element on front, dump back onto front, // but now we've already removed the head. return new AmortizedPQueue<E>(Empty.<E>stack().plusAll(back), Empty.<E>stack()); } else { // If there's more than one on front, we pop one off. return new AmortizedPQueue<E>(front.minus(0), back); } }
/* Worst-case O(1) */ public E peek() { if (size() == 0) { return null; } return front.get(0); }
/* Worst-case O(1) */ @Override public int size() { return front.size() + back.size(); }