package algorithm;
import bean.LinkSolution;
/**
* author : fzy
* date : 2019/11/11 8:30
* desc:用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
*/
public class demo5 {
public static void main(String[] args) {
LinkSolution solution = new LinkSolution();
for(int i=0;i<5;i++){
solution.push(i);
}
for(int i=0;i<5;i++){
System.out.println(solution.pop(i));
}
}
}
package bean;
import java.util.Stack;
/**
* author : fzy
* date : 2019/11/11 8:58
*/
public class LinkSolution {
private Stack<Integer> stack1 = new Stack<>();
private Stack<Integer> stack2 = new Stack<>();
public void push(int node){
while (!stack2.isEmpty()){
stack1.push(stack2.pop());
}
stack2.push(node);
}
public int pop(int node){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
return stack2.pop();
}
}