题目和示例

  1. 225. 用队列实现栈
  2. 请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppop empty)。
  3. 实现 MyStack 类:
  4. void push(int x) 将元素 x 压入栈顶。
  5. int pop() 移除并返回栈顶元素。
  6. int top() 返回栈顶元素。
  7. boolean empty() 如果栈是空的,返回 true ;否则,返回 false
  8. 注意:
  9. 你只能使用队列的基本操作 —— 也就是 push to backpeek/pop from frontsize is empty 这些操作。
  10. 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
  1. 示例:
  2. 输入:
  3. ["MyStack", "push", "push", "top", "pop", "empty"]
  4. [[], [1], [2], [], [], []]
  5. 输出:
  6. [null, null, null, 2, 2, false]
  7. 解释:
  8. MyStack myStack = new MyStack();
  9. myStack.push(1);
  10. myStack.push(2);
  11. myStack.top(); // 返回 2
  12. myStack.pop(); // 返回 2
  13. myStack.empty(); // 返回 False

代码

双队列1:

放的时候正常放入,取出来的时候先把dataQueue中的除了最后一个添加进来的所有元素放到tmpQueue队列中,然后通过poll()获得目的值,最后再通过交换两个队列将其他元素移回去dataQueue。

  1. class MyStack {
  2. // 存储数据的队列
  3. Queue<Integer> dataQueue = new LinkedList<>();
  4. // 临时队列
  5. Queue<Integer> tmpQueue = new LinkedList<>();
  6. // 记录栈顶元素
  7. int top;
  8. /**
  9. * Push element x onto stack.
  10. */
  11. public void push(int x) {
  12. dataQueue.add(x);
  13. top = x;
  14. }
  15. /**
  16. * Removes the element on top of the stack and returns that element.
  17. */
  18. public int pop() {
  19. while (dataQueue.size() > 1) {
  20. top = dataQueue.poll();
  21. tmpQueue.add(top);
  22. }
  23. int num = dataQueue.poll();
  24. // 再将dataQueue元素移回去
  25. Queue tmp = dataQueue;
  26. dataQueue = tmpQueue;
  27. tmpQueue = tmp;
  28. return num;
  29. }
  30. /**
  31. * Get the top element.
  32. */
  33. public int top() {
  34. return top;
  35. }
  36. /**
  37. * Returns whether the stack is empty.
  38. */
  39. public boolean empty() {
  40. return dataQueue.size() == 0;
  41. }
  42. }
  43. /**
  44. * Your MyStack object will be instantiated and called as such:
  45. * MyStack obj = new MyStack();
  46. * obj.push(x);
  47. * int param_2 = obj.pop();
  48. * int param_3 = obj.top();
  49. * boolean param_4 = obj.empty();
  50. */

双队列2:

a作为一个辅助队列, 放的时候保证了b队列始终是正常队列的相反顺序。

  1. class MyStack {
  2. private Queue<Integer> a;//输入队列
  3. private Queue<Integer> b;//输出队列
  4. public MyStack() {
  5. a = new LinkedList<>();
  6. b = new LinkedList<>();
  7. }
  8. public void push(int x) {
  9. a.offer(x);
  10. // 将b队列中元素全部转给a队列
  11. while(!b.isEmpty())
  12. a.offer(b.poll());
  13. // 交换a和b,使得a队列没有在push()的时候始终为空队列
  14. Queue temp = a;
  15. a = b;
  16. b = temp;
  17. }
  18. public int pop() {
  19. return b.poll();
  20. }
  21. public int top() {
  22. return b.peek();
  23. }
  24. public boolean empty() {
  25. return b.isEmpty();
  26. }
  27. }

单队列:

取的时候把除了最后一个元素的所有元素重新添加到队列中去。

  1. class MyStack {
  2. // 存储数据的队列
  3. Queue<Integer> dataQueue = new LinkedList<>();
  4. // 记录栈顶元素
  5. int top;
  6. /**
  7. * Push element x onto stack.
  8. */
  9. public void push(int x) {
  10. dataQueue.add(x);
  11. top = x;
  12. }
  13. /**
  14. * Removes the element on top of the stack and returns that element.
  15. */
  16. public int pop() {
  17. int cnt = 0;
  18. while (cnt < dataQueue.size() -1) {
  19. cnt++;
  20. top = dataQueue.poll();
  21. dataQueue.add(top);
  22. }
  23. return dataQueue.poll();
  24. }
  25. /**
  26. * Get the top element.
  27. */
  28. public int top() {
  29. return top;
  30. }
  31. /**
  32. * Returns whether the stack is empty.
  33. */
  34. public boolean empty() {
  35. return dataQueue.size() == 0;
  36. }
  37. }
  38. /**
  39. * Your MyStack object will be instantiated and called as such:
  40. * MyStack obj = new MyStack();
  41. * obj.push(x);
  42. * int param_2 = obj.pop();
  43. * int param_3 = obj.top();
  44. * boolean param_4 = obj.empty();
  45. */

用数组实现栈

public class MyStackByArray {

    // 存储数据
    int[] array;
    // 最大容量
    int maxSize;
    // 实际存储大小
    int top;

    public MyStackByArray(int size) {
        maxSize = size;
        array = new int[size];
        top = -1;
    }

    public void push(int value) {
        if (top < maxSize - 1) {
            top++;
            array[top] = value;
        }
    }


    public int pop() {
        int result = array[top];
        top--;
        return result;
    }

    public int peek() {
        return array[top];
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public boolean isFull() {
        return top == (maxSize - 1);
    }


    public static void main(String[] args) {
        MyStackByArray stack = new MyStackByArray(3);
        stack.push(1);
        stack.push(2);
        stack.push(3);

        System.out.println(stack.isFull());
        System.out.println(stack.peek());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.isEmpty());

    }
}