Implement queue and stack conversion

What is the difference between stack and queue?

A stack is a special linear list that only allows insertion and deletion of elements at a fixed end. The data elements in the stack follow the last-in-first-out principle. As shown in the figure, the element at the top of the stack is popped off the stack first, and the last element popped off the stack is the first element entered into the stack.

                              

A queue is also a special linear table, but it only allows data to be inserted at one end and deleted from the other end. The data elements of the queue follow the first-in-first-out principle. The end that performs the insertion operation is called the tail of the queue, and the end that performs the deletion operation is called the head.

                            

Example 1: Use two queues to implement a last-in-first-out stack

There are two queues, queue1 is used to store elements in the stack, and queue2 is used as an auxiliary queue for push operations. When pushing onto the stack, first push the elements onto queue2, and then dequeue all the elements in queue1 one by one and merge them into queue2. At this time, the front-end element of queue2 is the newly pushed element. Then, swap queue1 and queue2, so that the elements of queue1 are elements in the stack, and the front end and back end of queue1 correspond to the top and bottom of the stack respectively.

class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;

    //初始化两个队列的数据结构
    public MyStack() {
        queue1 = new LinkedList<Integer>();
        queue2 = new LinkedList<Integer>();
    }
    
   //将元素x入栈到栈中
    public void push(int x) {
        queue2.offer(x);//将元素入栈到queue2
        while (!queue1.isEmpty()) {
            queue2.offer(queue1.poll());//将queue1中的元素出队,入栈到queue2中
        }
        Queue<Integer> temp = queue1;//将queue2中的全部元素入栈到queue1
        queue1 = queue2;
        queue2 = temp;
    }
    
    //将栈顶的元素删除,并返回该值
    public int pop() {
        return queue1.poll();
    }
    
   //获得栈顶的元素
    public int top() {
        return queue1.peek();
    }
    
   //栈中是否为空
    public boolean empty() {
        return queue1.isEmpty();
    }
}

Example 2: Use two stacks to implement a first-in, first-out queue

Two stacks, one as the input stack and the other as the output stack. Every time you pop the stack or view the top element of the stack, if the output stack is empty, all elements of the input stack will be popped and pushed to the output stack. In this way, the order of the output stack from the top of the stack to the bottom of the stack is the order of the queue from the beginning to the end of the queue.

class MyQueue {
    Deque<Integer> inStack;
    Deque<Integer> outStack;

    public MyQueue() {//初始化两个栈
        inStack = new ArrayDeque<Integer>();
        outStack = new ArrayDeque<Integer>();
    }

    public void push(int x) {
        inStack.push(x);
    }

    public int pop() {
        if (outStack.isEmpty()) {//如果输出栈为空,则将输入栈的全部元素弹出,入栈到输出栈
            in2out();
        }
        return outStack.pop();
    }

    public int peek() {//查看拿到栈顶元素 
        if (outStack.isEmpty()) {
            in2out();
        }
        return outStack.peek();
    }

    public boolean empty() {
        return inStack.isEmpty() && outStack.isEmpty();
    }

    private void in2out() {
        while (!inStack.isEmpty()) {//如果输入栈不为空,则将全部元素入栈到输出栈
            outStack.push(inStack.pop());
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_72000264/article/details/130154896