![](/img/trans.png)
[英]how can i call java.util.queue poll() method and not remove elements in my queue
[英]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.