簡體   English   中英

如何修復隊列的“刪除”方法

[英]How can I fix my queue's “remove” method

我的隊列使用一個堆棧,因此我有兩個堆棧s1接受添加,然后將其所有項目移到s2 ,這使s2成為隊列。

(不使用數組..)

這是我的實現,但是當我測試它時,刪除單元測試失敗。

public class Queue
{
    private Stack s1;
    private Stack s2;

    private int size;


    public Queue()
    {
        //arbitrary sized.
        s1 = new Stack();
        s2 = new Stack();

        size = 0;
    }

    public void insert(Object o)
    {
        //add object into s1.
        s1.push(o);

        size++;
    }

    //delete from queue
       public Object remove()
       {
          int n = 0; ... arbitrary size n. //size not specified
          for(int i = 1; i <= n ; i++)
         {
           //push all elements in s1 into s2 
           s2.insert(s1.pop());
         }                 

          //decrease the size
          size--;

         return s2.pop; 
       }
    public Object peekFront()
    {
        s2.push(s1.pop());


        return s2.peek();
    }


}

測試

import org.junit.Assert;
import static org.junit.Assert.*;
import org.junit.Test;


public class QueueTest 
{
   protected Queue q;

   public QueueTest()
   {
      q = new Queue();
   }

   atTest
   public void testRemove()
       {
         assertTrue(q.isEmpty()); -- passes

         q.insert(10);
         q.insert(11);
         q.insert(12);
         q.insert(23);

         //remove
         assertEquals(10, q.remove()); --- fails

       }


   public void testPeekFront()
   {
     q.insert(80);
     q.insert(90);
     q.insert(57);

     assertEquals(20,q.peekFront());

   }
}

請您向我說明為什么我的public Object remove功能無法正常運行的正確方向...

例如,當我嘗試刪除23嗎? 我的測試通過了,但是當我測試實際應該達到的10個時,它就失敗了。

這是類和測試的完整代碼.....

我認為您可能為n提供了靜態值。 由於值n會動態變化,請確保您提供n = s1.size()[或任何用於計算尺寸的自定義函數]。 請提供完整的代碼。

假定的修復:
1.在刪除過程中,您將從s1中彈出所有元素。 使它成為空堆棧。 因此,您必須通過在remove函數本身中彈出s2中的值來填充它。 正如Fabian在下一個答案中提到的那樣,使用輔助方法將元素從一個堆棧轉移到另一個堆棧。
2在remove()方法中,將s1.pop()存儲到一個臨時變量中並刪除s2中的所有元素。 返回臨時變量。 其他明智的s2將繼續增長。
3.設置n = s1.size();
4.返回s1.pop();

為了使該方法起作用,您不能簡單地對大小進行硬編碼。 此外,您必須在下一次插入之前將元素移回原始堆棧,否則順序會出錯。 我建議使用輔助方法傳輸對象,以避免代碼重復:

private static void transfer(Stack source, Stack target) {
     while (!source.isEmpty()) {
         target.push(source.pop());
     }
}

我建議延遲移動元素,以避免重復insert或重復remove操作不必要的操作:

public void insert(Object o) {
    // lazily transfer values back
    transfer(s2, s1);

    //add object into s1.
    s1.push(o);

    size++;
}

//delete from queue
public Object remove() {
    if (s1.isEmpty() && s2.isEmpty()) {
         return null; // alternative: throw exception
    }
    transfer(s1, s2);
    //decrease the size
    size--;
    return s2.pop();
}

public Object peekFront() {
    if (s1.isEmpty() && s2.isEmpty()) {
        return null; // alternative: throw exception
    }

    transfer(s1, s2);

    return s2.peek();
}

或者,您可以在remove方法中將值簡單地傳遞回s1 ,這將使實現其他操作更加簡單,但是也會使某些操作序列的效率降低。 (您還需要修復peekFront() ):

//delete from queue
public Object remove() {
    if (s1.isEmpty() && s2.isEmpty()) {
         return null; // alternative: throw exception
    }
    transfer(s1, s2);

    //decrease the size
    size--;

    Object result = s2.pop();

    transfer(s2, s1);
    return result;
}

暫無
暫無

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

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