225. [栈]用队列实现栈
225. 用队列实现栈
方法一:单链表实现栈
class MyStack {
LinkedList<Integer> stack ;
/** Initialize your data structure here. */
public MyStack() {
stack = new LinkedList<Integer>();
}
/** Push element x onto stack. */
public void push(int x) {
stack.offerLast(x);
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return stack.pollLast();
}
/** Get the top element. */
public int top() {
return stack.peekLast();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return stack.isEmpty();
}
}

浙公网安备 33010602011771号