225. 用队列实现栈

  1. class MyStack {
  2. Deque<Integer> deque;
  3. /** Initialize your data structure here. */
  4. public MyStack() {
  5. deque = new LinkedList<>();
  6. }
  7. /** Push element x onto stack. */
  8. public void push(int x) {
  9. deque.push(x);
  10. }
  11. /** Removes the element on top of the stack and returns that element. */
  12. public int pop() {
  13. return deque.pop();
  14. }
  15. /** Get the top element. */
  16. public int top() {
  17. return deque.peek();
  18. }
  19. /** Returns whether the stack is empty. */
  20. public boolean empty() {
  21. return deque.isEmpty();
  22. }
  23. }
  24. /**
  25. * Your MyStack object will be instantiated and called as such:
  26. * MyStack obj = new MyStack();
  27. * obj.push(x);
  28. * int param_2 = obj.pop();
  29. * int param_3 = obj.top();
  30. * boolean param_4 = obj.empty();
  31. */