Jianzhi Offer 09 implements queues with two stacks

At first, I didn't understand the meaning of the question when I read the question. I only understood after seeing the vernacular explanation in the solution.

The characteristics of stack (first in first out) and queue (first in first out) should be investigated.

There are two stacks here. My understanding is that the first is the function stack, and the second is the auxiliary stack. According to the characteristics of the stack, two stacks are used to realize the function of the queue.

My personal summary is that, first, numbers are pushed into the first stack. At this time, stack 1 is empty, and stack 2 is empty . For example, enter the three numbers of 1, 3, and 5 in sequence. At this time, stack 1 and stack 2 are shown in the figure.

If you want to delete the head element at this time, that is, the first entered number: 1, then you need to pop the three elements of stack 1 one by one (pop) and push them into stack 2 (push). At this time, stack 1 is empty, stack 2 is not empty , according to the principle of first in, last out, stack 2 becomes as shown in the figure

            

Then pop 1 (pop), so that the deletion of the head element can be realized

Therefore, if it is to implement adding elements, then all elements are first pushed into stack 1

Then delete the element to consider the situation of stack 2, because in order to delete the head element, the element of stack 1 must be popped first, and then pushed into stack 2. At this time, stack 2 is not empty, indicating that the elements are all in stack 2, and the deletion operation It can be executed directly and sequentially, and no additional consideration is required.

According to the meaning of the question, if the stacks 1 and 2 are both empty, the delete operation returns -1

Then consider the situation of stack 1 and stack 2

①, stack 2 is empty, stack 1 is not empty

In this case, a new element is pushed into stack 1. At this time, the situation of stack 1 and stack 2 is shown in the figure

Then the head element at this time becomes 7. If you want to delete 7, first pop the element of stack 2 and push it into stack 1, and then pop the element of stack 1 and push it into stack 2, then 7 is at the top of stack 2, so The delete operation can be implemented.

Stack 2 elements are popped and pushed onto stack 1 in turn:

Stack 1 elements are popped and pushed onto stack 2 in turn:

②, stack 1 is empty, stack 2 is not empty

Pop the top element of stack 2 directly and delete it

At this point, the solution of my personal ideas is over, and the code is written according to the ideas

class CQueue {
    
    private Stack<Integer> stack1;
    private Stack<Integer> stack2;

    public CQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }
    
    public void appendTail(int value) {
        stack1.push(value);
    }

    public int deleteHead() {
        // 栈2为空
        if (stack2.isEmpty()) {
            //栈2为空、栈1不为空
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        //栈2空、栈1空
        if (stack2.isEmpty()) {
            return -1;
        } else { //栈2不空
            return stack2.pop();
        }
    }
}

isEmpty() determines whether it is empty, returns true if the stack is empty, and returns false if the stack contains elements

Guess you like

Origin blog.csdn.net/Sean_Asu/article/details/122884577