簡體   English   中英

為什么我的dequeue方法不能用於我的treeMap PriceQueue?

[英]Why isn't my dequeue method working for my treeMap PriceQueue?

我有一個PriceQueue類,我已經通過了所有的測試,除了2.失敗的Thenones是:

public void test05DeleteBackInOrder() {
    PriceQueue pq = new PriceQueue();
    pq.enqueue(p101);
    pq.enqueue(p202);
    pq.enqueue(p303);
    pq.enqueue(p404);
    pq.enqueue(p505);
    assertTrue(pq.delete(p303));
    assertTrue(pq.delete(p404));
    assertTrue(pq.delete(p505));
    assertEquals(p101, pq.dequeue());
    assertEquals(p202, pq.dequeue());

    pq = new PriceQueue();
    pq.enqueue(p101);
    pq.enqueue(p202);
    pq.enqueue(p303);
    pq.enqueue(p404);
    pq.enqueue(p505);
    assertTrue(pq.delete(p303));
    assertTrue(pq.delete(p404));
    assertTrue(pq.delete(p505));
    pq.enqueue(p303);
    pq.enqueue(p404);
    assertEquals(p101, pq.dequeue());
    assertEquals(p202, pq.dequeue());
    assertEquals(p303, pq.dequeue()); //This is where it is failing
    assertEquals(p404, pq.dequeue());
}

public void test05DeleteMiddleInOrder() {
    PriceQueue pq = new PriceQueue();
    pq.enqueue(p101);
    pq.enqueue(p202);
    pq.enqueue(p303);
    pq.enqueue(p404);
    pq.enqueue(p505);
    assertTrue(pq.delete(p202));
    assertTrue(pq.delete(p303));
    assertTrue(pq.delete(p404));
    assertEquals(p101, pq.dequeue());
    assertEquals(p505, pq.dequeue()); // This is where it fails

    pq = new PriceQueue();
    pq.enqueue(p101);
    pq.enqueue(p202);
    pq.enqueue(p303);
    pq.enqueue(p404);
    pq.enqueue(p505);
    assertTrue(pq.delete(p202));
    assertTrue(pq.delete(p303));
    assertTrue(pq.delete(p404));
    pq.enqueue(p202);
    pq.enqueue(p303);
    assertEquals(p101, pq.dequeue());
    assertEquals(p505, pq.dequeue());
    assertEquals(p202, pq.dequeue());
    assertEquals(p303, pq.dequeue());
}

這些是失敗的,我不明白我錯過了刪除prie以便它正確出列的地方。 這是我的代碼:

public Price dequeue() {
    if (isEmpty()) throw new NoSuchElementException("Queue underflow");
    Price price = first.price;
    first = first.next;
    n--;
    if (isEmpty()) last = null; 
    hold.remove(hold.lastKey());
    // to avoid loitering
    return price;
}


/**
 * Deletes a Price from the queue if it was present.
 * @param price the Price to be deleted.
 * @return {@code true} if the Price was deleted and {@code false} otherwise
 */
public boolean delete(Price price) {
    // TODO implelment me!!!
    // Make sure the running time is no worse than logrithmic!!!
    // You will want to use Java's TreeMap class to map Prices to the node
    // that precedes the Price in the queue
    //last node==special case
    //^^ requires resetting prev. to null, and val to next
    if (hold.containsKey(price)) {
        Node temp = hold.get(price);
        //if (price.equals(f))
        if (price.equals(first.price) && n >= 3) {
            first = first.next;
            n--;
            hold.remove(price);
            return true;
        }
        if (price.equals(first.price)) {
            first = first.next;
            hold.remove(price);
            n--;
            return true;
        }
        if (price.equals(last.price)) {
            temp.next = null;
            last = temp;
            n--;
            hold.remove(price);
            return true;
        }
        if (temp.next != (null)) {
            temp.next = temp.next.next;
            n--;
            hold.remove(price);
            return true;
        }

        return true;
    }
    else return false;

}

任何幫助即使是小費或暗示也會受到贊賞。 如果需要其他任何東西請告訴我,我會提供,這些只是我的代碼的一部分。 謝謝!

關於標准出隊的快速回答是簡單地返回remove的輸出。

public Price dequeue(){
    return some_list.remove(0); //null if empty
}

Remove返回它刪除的元素,並自動將刪除的項目1后面的元素移到左側。 如果該項不存在(列表為空),則它將返回null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM