Use two queues to implement the stack function

Queue implementation stack

1. Problem solving ideas

  • First, clarify the characteristics of the stack and the queue: the stack is first in last out , and the queue is first in first out ;
  1. Suppose there are two queues, qu1 and qu2;
  2. Now you have a set of numbers [12, 23, 45, 55, 35];
  3. You first enter 12, 23, and 34 into the queue qu2, because the function of the stack is implemented, and now the stack operation is required. The stack is last-in, first-out, so the first to go out is 34;
  4. But now 34 is at the end of the queue, so do you have to move 12 and 34 to the next queue qu1 first, and just pop the remaining 34 of qu2 directly;
  5. Suppose you want to play 23 now, the same principle; (provided that the other queue is empty)
  6. Suppose you pop up 34 and don't pop up 23, but continue to add numbers, then which queue should be added;
  7. Now qu1 has a number, then you will join qu1 (which queue is not empty, join which one);
    Insert picture description here

2. Concrete realization

2.1 Join the team
//入队
    public void push(int x) {
    
    
        //1、谁不为空  入到哪个队列就好了
        //2、两个都为空   放到第一个qu1当中
        if (!qu1.isEmpty()){
    
    
            qu1.offer(x);
        }else if (!qu2.isEmpty()){
    
    
            qu2.offer(x);
        } else {
    
    
            qu1.offer(x);
        }
        usedSize++;
    }
2.2 Departure
在这里插入代码片//出队
    public int pop() {
    
    
        //1.先将前面size-1个移动到空的队列
        //2.将队列最后一个弹出,就是类似于出栈顺序
        //3.反复进行,直到俩个队列都为空的时候出队完毕
        if (empty()){
    
    //俩个队列都空
            return -1;
        }
        int size = usedSize;
        if (!qu1.isEmpty()){
    
    
            for (int i = 0; i < size-1; i++) {
    
    
                qu2.offer(qu1.poll());
            }
            usedSize--;
            return qu1.poll();
        }else{
    
    

            for (int i = 0; i < size-1; i++) {
    
    
                qu1.offer(qu2.poll());
            }
            usedSize--;
            return qu2.poll();
        }

    }
2.3 Pop the top of the stack
 //弹栈顶
    public int top() {
    
    
        if (empty()){
    
    //俩个队列都空
            return -1;
        }
        int tmp = -1;
        int size = usedSize;
        if (!qu1.isEmpty()){
    
    
            for (int i = 0; i < size ; i++) {
    
    
                tmp = qu1.poll();
                qu2.offer(tmp);
            }
        }else{
    
    
            for (int i = 0; i < size; i++) {
    
    
                tmp = qu2.poll();
                qu1.offer(tmp);
            }
        }
        return tmp;
    }
2.4 Determine whether it is empty
public boolean empty() {
    
    
        if(qu1.isEmpty() && qu2.isEmpty()){
    
    
            return true;
        }
        return false;
    }

3. Complete code

import java.util.LinkedList;
import java.util.Queue;

//用队列实现栈(需要俩个队)
class MyStack {
    
    

    private Queue<Integer> qu1;
    private Queue<Integer> qu2;
    public int usedSize;


    /** Initialize your data structure here. */
    //构造方法
    public MyStack() {
    
    
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();

    }

    /** Push element x onto stack. */
    //入队
    public void push(int x) {
    
    
        //1、谁不为空  入到哪个队列就好了
        //2、两个都为空   放到第一个qu1当中
        if (!qu1.isEmpty()){
    
    
            qu1.offer(x);
        }else if (!qu2.isEmpty()){
    
    
            qu2.offer(x);
        } else {
    
    
            qu1.offer(x);
        }
        usedSize++;
    }

    /** Removes the element on top of the stack and returns that element. */
    //出队
    public int pop() {
    
    
        //1.先将前面size-1个移动到空的队列
        //2.将队列最后一个弹出,就是类似于出栈顺序
        //3.反复进行,直到俩个队列都为空的时候出队完毕
        if (empty()){
    
    //俩个队列都空
            return -1;
        }
        int size = usedSize;
        if (!qu1.isEmpty()){
    
    
            for (int i = 0; i < size-1; i++) {
    
    
                qu2.offer(qu1.poll());
            }
            usedSize--;
            return qu1.poll();
        }else{
    
    

            for (int i = 0; i < size-1; i++) {
    
    
                qu1.offer(qu2.poll());
            }
            usedSize--;
            return qu2.poll();
        }

    }

    /** Get the top element. */
    //弹栈顶
    public int top() {
    
    
        if (empty()){
    
    //俩个队列都空
            return -1;
        }
        int tmp = -1;
        int size = usedSize;
        if (!qu1.isEmpty()){
    
    
            for (int i = 0; i < size ; i++) {
    
    
                tmp = qu1.poll();
                qu2.offer(tmp);
            }
        }else{
    
    
            for (int i = 0; i < size; i++) {
    
    
                tmp = qu2.poll();
                qu1.offer(tmp);
            }
        }
        return tmp;
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
    
    
        if(qu1.isEmpty() && qu2.isEmpty()){
    
    
            return true;
        }
        return false;
    }
}


public class TestDemo2 {
    
    
    public static void main(String[] args) {
    
    
        MyStack myStack = new MyStack();
        myStack.push(10);
        myStack.push(20);
        myStack.push(30);
        System.out.println(myStack.top());
    }
}


Guess you like

Origin blog.csdn.net/qq_45665172/article/details/110219169