Two queues to achieve a stack + two stacks to achieve a queue (Java implementation)

Two queues implement a stack

  1. There are two queues q1 and q2, and the elements are added to q1 when pushed onto the stack
  2. When popping the stack, first judge whether q1 is empty, because the elements in q1 are always last in, last in first out, except for the last element of the queue, add other elements to q2, and the last element of q1 is dequeued
  3. When popping from the stack, if it is judged that q1 is empty in 2, except for the last element of q2, add other elements in q2 to q1, and then the last element in q2 is dequeued

Complete Java code:

public class MyStack {
    
    

    private Queue<Integer> q1 = new LinkedList<>();
    private Queue<Integer> q2 = new LinkedList<>();

    public MyStack() {
    
    
    }

    public boolean push(Integer num) {
    
    
        return q1.offer(num);
    }

    public Integer pop() {
    
    
        if (q1.isEmpty() && q2.isEmpty()) {
    
    
            return null;
        }
        // 先判断 q1 是否为空 
        if (!q1.isEmpty()) {
    
    
            int size = q1.size();
            for (int i = 0; i < size - 1; i++) {
    
    
                q2.offer(q1.poll());
            }
            return q1.poll();
        } else {
    
    
            int size = q2.size();
            for (int i = 0; i < size - 1; i++) {
    
    
                q1.offer(q2.poll());
            }
            return q2.poll();
        }
    }

    public Integer size() {
    
    
        return q1.size() + q2.size();
    }


    public static void main(String[] args) {
    
    
        MyStack stack = new MyStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);

        int size = stack.size();
        for (int i = 0; i < size; i++) {
    
    
            System.out.print(stack.pop() + " ");
        }
        System.out.println();
        stack.push(6);
        System.out.println(stack.pop());
    }
}

Two stack implementation queue

Similar to the queue implementation, but much simpler than the two queue implementation stacks.

  1. There are two stacks stack1 and stack2
  2. Just add elements to stack1 when entering the queue
  3. When leaving the queue, first judge whether stack2 is empty. The elements in stack2 are first in, first in, first out. If stack2 is not empty, pop the top element of stack2 directly. If it is empty, add the element of stack1 to stack2, and then pop the top element of stack2

Look at the Java code:

public class MyQueue {
    
    
    private Stack<Integer> stack1 = new Stack<>();
    private Stack<Integer> stack2 = new Stack<>();

    /**
     * 添加元素到队列
     *
     * @param num
     * @return
     */
    public Integer add(Integer num) {
    
    
        return stack1.push(num);
    }

    /**
     * 队列弹出元素
     *
     * @return
     */
    public Integer poll() {
    
    
        if (stack1.isEmpty() && stack2.isEmpty()) {
    
    
            return null;
        }
        // 先判断 stack2
        if (!stack2.isEmpty()) {
    
    
            return stack2.pop();
        }
        // 将 stack1 的元素放入 stack2
        int size = stack1.size();
        for (int i = 0; i < size; i++) {
    
    
            stack2.push(stack1.pop());
        }
        return stack2.pop();
    }

    public int size() {
    
    
        return stack1.size() + stack2.size();
    }


    public static void main(String[] args) {
    
    
        MyQueue queue = new MyQueue();
        queue.add(1);
        queue.add(2);
        queue.add(3);
        queue.add(4);
        queue.poll();
        queue.add(5);
        int size = queue.size();

        for (int i = 0; i < size; i++) {
    
    
            System.out.print(queue.poll() + " ");
        }
        System.out.println();
    }
}

Guess you like

Origin blog.csdn.net/jiaobuchong/article/details/88623605