Use two stacks to implement the push and pop operations of the queue (Java)

topic analysis

Stack: The basic data structure that satisfies the first-in-last-out feature.
Queue: Just like queuing, first in first out.

Idea: According to the characteristics of the stack and the characteristics of the queue, use a stack to save data, and a stack to assist the push operation of data.

Code

package stackQueueAlgorithm;

import java.util.Stack;

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

    public void  push(Integer node){
    
    
        //stack1作为辅助栈进行入队操作
        //stack2作为主栈用来保存已经入队的数据
        if(stack2.isEmpty()){
    
    
            stack2.push(node);
        }else{
    
    
            //stack2不为空,就将所有的数据依次弹出压入stack1
            while(!stack2.isEmpty()){
    
    
                stack1.push(stack2.pop());
            }
            stack2.push(node);
            //将stack1的数据全部压回来
            while(!stack1.isEmpty()){
    
    
                stack2.push(stack1.pop());
            }
        }

    }

    public int pop(){
    
    
       //出栈弹出stack2栈顶的数据即可
        return stack2.pop();
    }

}

Since the Stack in Java inherits from Vector, and the JRT library method used has fully considered exceptions and various conditions, the pop method only needs to pop the top data of the stack.

Guess you like

Origin blog.csdn.net/weixin_42643321/article/details/108516977