Esempio n. 1
0
  /* 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);
    }
  }
Esempio n. 2
0
 private AmortizedPQueue() {
   front = Empty.<E>stack();
   back = Empty.<E>stack();
 }