Utilice la propia pila de Java para implementar operaciones de cola
class MyQueue {
Stack<Integer> stack ;//定义成员变量stack
/** Initialize your data structure here. */
public MyQueue()//构造方法,初始化栈
{
stack = new Stack<Integer>();
}
/** Push element x to the back of queue. */
public void push(int x)
//完成入队操作,先将所有元素出栈,将此元素入栈,再反序将所有元素入栈
{
int size = stack.size();
int m[] = new int[size];
for(int i=0;i<size;i++)
{
m[i]=stack.peek();
stack.pop();
}
stack.push(x);
for(int i=size-1;i>=0;i--)
{
stack.push(m[i]);
}
}
/** Removes the element from in front of queue and returns that element. */
public int pop()
{
return stack.pop();
}
/** Get the front element. */
public int peek()
{
return stack.peek();
}
/** Returns whether the queue is empty. */
public boolean empty()
{
return stack.isEmpty();
}
}