Realize stack with queue based on java

Realize stack with queue based on java

Problem Description

Please use only two queues to implement a last-in-first-out (LIFO) stack, and support all four operations (push, top, pop, and empty) of a normal stack.

Implement the MyStack class:

void push(int x) pushes the element x onto the top of the stack.
int pop() removes and returns the top element of the stack.
int top() Returns the top element of the stack.
boolean empty() Returns true if the stack is empty; otherwise, returns false.

Notice:

You can only use the basic operations of the queue - that is, push to back, peek/pop from front, size and is empty
. Your language may not support queues. You can use list (list) or deque (double-ended queue) to simulate a queue, as long as it is a standard queue operation.

example

insert image description here

Original title OJ link

https://leetcode.cn/problems/implement-stack-using-queues/

answer

class MyStack {
    
    

    Queue<Integer> que1;
    Queue<Integer> que2;

    public MyStack() {
    
    
        que1 = new LinkedList<>();
        que2 = new LinkedList<>();
    }

    public void push(int x) {
    
    
        if(!que1.isEmpty()){
    
    
            que1.offer(x);
        }else{
    
    
            if (!que2.isEmpty()){
    
    
                que2.offer(x);
            }
            else{
    
    
                que1.offer(x);
            }
        }
    }

    public int pop() {
    
    
        if(!que1.isEmpty()){
    
    
            /*for (int i = 0; i < que1.size()-1; i++) {
                int ret = que1.poll();
                que2.offer(ret);
            }*/
            //这样写是错误的,因为que1.size()随着弹出元素是会变化的
            int size = que1.size();
            for (int i = 0; i < size-1; i++) {
    
    
                int ret = que1.poll();
                que2.offer(ret);

            }
            return que1.poll();
        }
        else {
    
    
            if(!que2.isEmpty()){
    
    
                int size = que2.size();
                for (int i = 0; i < size-1; i++) {
    
    
                    int ret = que2.poll();
                    que1.offer(ret);
                }
                return que2.poll();
            }else{
    
    
                return -1;
            }
        }
    }

    public int top() {
    
    

        if(!que1.isEmpty()){
    
    
            int size = que1.size();
            int ret = 0;
            for (int i = 0; i < size; i++) {
    
    
                ret = que1.poll();
                que2.offer(ret);
            }
            return ret;
        }
        else {
    
    
            if(!que2.isEmpty()){
    
    
                int size = que2.size();
                int ret = 0;
                for (int i = 0; i < size; i++) {
    
    
                    ret = que2.poll();
                    que1.offer(ret);
                }
                return ret;
            }else{
    
    
                return -1;
            }
        }

    }

    public boolean empty() {
    
    
        return que1.isEmpty() && que2.isEmpty();

    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

Test Results

insert image description here

Guess you like

Origin blog.csdn.net/baixian110/article/details/130884417