我正在尝试使用队列实现堆栈。我正在使用2个队列来实现。我收到错误"此处不允许使用Void类型" in" top = q1.remove()"在pop()函数中。
我应该在此代码中修改什么? 提前谢谢。
class MyStack {
/** Initialize your data structure here. */
private Queue<Integer> q1=new LinkedList<Integer>();
private Queue<Integer> q2=new LinkedList<Integer>();
private int top;
/** Push element x onto stack. */
public void push(int x) {
q1.add(x);
top=x;
}
/** Removes the element on top of the stack and returns that element. */
public void pop() {
while(q1.size()>1){
top=q1.remove();
q2.add(top);
}
q1.remove();
Queue<Integer> temp=q1;
q1=q2;
q2=temp;
}
/** Get the top element. */
public int top() {
return top;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return q1.isEmpty();
}
}