简体   繁体   中英

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

My queue uses a stack hence I have two stacks.. s1 that accepts the add, then all of its items get moved into s2 which makes s2 my queue.

(no use of arrays..)

here is my implementation but when I test it, my remove unit test fails.

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();
    }


}

TEST

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());

   }
}

Please can you put me in the right direction on why my public Object remove is not functioning correctly...

For example when I try to remove 23? My test passes but when I test for 10 which actually should be, then it fails.

Here is the complete code..... for both the class and the test...

I think you may be providing a static value for n. As the value n , will change dynamically, please make sure you are giving n=s1.size() [or any custom function to calculate size]. Please provide the complete code.

Assumed Fixes:
1. You are popping out all elements from s1 , during remove. Making it(s1) empty stack. So you have to fill it back by popping the values from s2 in the remove function itself. As fabian mentioned in the next answer use a helper method for transfering of elements from 1 stack to another.
2.In remove() method, store s1.pop() to a temp variable and remove all elements in s2. return temp variable. Other wise s2 will keep on growing.
3. set n = s1.size();
4. return s1.pop();

For the method to work you cannot simply hardcode the size. Furthermore you have to move the elements back to the original stack before the next insertion, or the order becomes wrong. I recommend transfering the objects in a helper method to avoid code duplication:

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

I recommend moving the elements lazily to avoid unnecessary operations for repeated insert or repeated remove operations:

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();
}

Alternatively you could simply transfer the values back to s1 in the remove method, which would make it a bit simpler to implement additional operations, however it would also make some operation sequences less efficient. (You also still need to fix 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM