用队列来模拟栈.....可以在每次push的时候都将队列的前面所有元素重新插入一遍,这样始终保持这个队列的顺序按照栈的顺序排列即可
class MyStack {
// Push element x onto stack.
Queue<Integer> queue=new LinkedList<Integer>();
public void push(int x) {
queue.add(x);
for( int i=0;i<queue.size()-1;i++ )
{
queue.add( queue.poll() );
//queue.remove();
}
}
// Removes the element on top of the stack.
public void pop() {
queue.remove();
}
// Get the top element.
public int top() {
return queue.peek();
}
// Return whether the stack is empty.
public boolean empty() {
return queue.isEmpty();
}
}