package com.example.demo.subject1;
import java.util.Stack;
/**
* @version: 1.00.00
* @description:用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
* @author: zrh
* @date 2021/4/19 22:27
*/
public class MyQueue {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
/**
* 当进栈的时候,把2栈剩余的数据放在1栈里
* @param node
*/
public void push(int node) {
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
stack1.push(node);
}
/**
* 出栈的时候将一栈的数据再放到二栈;里即可
* @return
*/
public int pop() {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
return stack2.pop();
}
}