Question # 255 leetcode stack implemented with queue

For details, see the topic here .

topic

Queue implementation using the following stack:

  • push (x) - stack element x
  • pop () - remove the top element
  • top () - Gets the top element
  • empty () - Returns whether the stack is empty

note:

You can only use the basic operation of the queue - that is push to back, peek/pop from front, size, and is emptythese operations are legal.
The language you are using may not support queue. You can use the list or the deque (deque) to simulate a queue, the queue as long as the operation to the standard.
You may assume that all operations are valid (for example, to an empty stack does not call the pop operation or top).

Problem-solving ideas

You should understand the different characteristics of the stack and queue two data structures, the stack is the last out, is a FIFO queue, if the queue to use the stack to achieve, you need to make the first team queue elements remain as the top element, and the elements in the queue of a team order to maintain consistency with the stack.

My idea is to use problem-solving to achieve two queues, a queue (hereinafter referred to as " top of the stack queue ") to store data stack, a queue (hereinafter referred to as " non-stack queue ") to save the non-stack data but team order is consistent with the order of the stack. When the data stack the stack is determined whether the queue is empty, empty enqueue data; determining whether the non-stack or queue is empty, the empty data queue enqueue non-stack, and the stack and non-stack queue top exchange queue; if both queues are not empty, a non-stack data queue to enqueue sequence queue stack, and a non-empty queue stack, the stack data queue enqueue to a non-stack, and finally the two queues exchange.

Note that the pop operation, when the queue before the top of the stack pop operations, you need to stack non-first team queue elements incorporated into the team to the team top of the stack queue, or stack pop operations after the queue will be empty .

Code

import java.util.concurrent.LinkedBlockingQueue;

class MyStack {

    private Queue<Integer> tempQueue;
    private Queue<Integer> notTopStackQueue;
    private Queue<Integer> stackQueue;

    public MyStack() {
        notTopStackQueue = new LinkedBlockingQueue<>();
        stackQueue = new LinkedBlockingQueue<>();
    }

    public void push(int x) {
        if(stackQueue.isEmpty()) {
            stackQueue.add(x);
        } else if(notTopStackQueue.isEmpty()) {
            notTopStackQueue.addAll(stackQueue);
            stackQueue.clear();
            stackQueue.add(x);
        } else {
            stackQueue.addAll(notTopStackQueue);
            notTopStackQueue.clear();

            // swap stackQueue with notTopStackQueue
            tempQueue = stackQueue;
            stackQueue = notTopStackQueue;
            notTopStackQueue = tempQueue;

            stackQueue.add(x);
        }
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        if(!notTopStackQueue.isEmpty()) {
            stackQueue.add(notTopStackQueue.poll());
        }
        return stackQueue.poll();
    }

    /** Get the top element. */
    public int top() {
        return stackQueue.peek();
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return stackQueue.isEmpty();
    }
}

Reproduced in: https: //www.jianshu.com/p/db3d0d76e714

Guess you like

Origin blog.csdn.net/weixin_34128501/article/details/91195738