《剑指offer》用两个队列实现栈 Java版实现
来源:互联网 发布:办公楼网络布线图视频 编辑:程序博客网 时间:2024/05/19 17:23
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class QueneImplementByTwoStacks {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int in1=sc.nextInt();
int in2=sc.nextInt();
int in3=sc.nextInt();
int in4=sc.nextInt();
int in5=sc.nextInt();
QueneImplementByTwoStacks queue = new QueneImplementByTwoStacks();
List<Integer> re =new ArrayList<Integer>();
queue.offer(in1);
queue.offer(in2);
queue.offer(in3);
re.add(queue.poll());
queue.offer(in4);
re.add(queue.poll());
queue.offer(in5);
re.add(queue.poll());
System.out.println(re.toString());
}
private Stack<Integer> stack1;
private Stack<Integer> stack2;
QueneImplementByTwoStacks(){
stack1=new Stack<Integer>();
stack2=new Stack<Integer>();
}
public Integer poll(){
Integer re = null;
if(!stack2.empty()){
re=stack2.pop();
}else{
while(!stack1.empty()){
re=stack1.pop();
stack2.push(re);
}
if(!stack2.empty()){
re=stack2.pop();
}
}
return re;
}
public Integer offer(int o){
stack1.push(o);
return o;
}
}
- 《剑指offer》用两个队列实现栈 Java版实现
- 【剑指offer-Java版】07用两个栈实现队列
- 剑指offer-----用两个栈实现队列(java版)
- 剑指offer:用两个栈实现队列、用两个队列实现一个栈(java)
- 剑指offer-Java-用两个栈实现队列
- 【剑指offer】 用两个栈实现队列 -Java
- 《剑指offer》牛客网java题解-用两个栈实现队列
- [剑指offer]用两个栈实现队列
- 【剑指offer】用两个栈实现队列
- 剑指offer--用两个栈实现队列
- 剑指Offer-用两个栈实现队列
- 剑指offer:用两个栈实现队列
- 《剑指offer》用两个栈实现队列
- 用两个栈实现队列(剑指offer)
- 剑指 offer:用两个栈实现队列
- 【剑指Offer】用两个栈实现队列
- 剑指offer-用两个栈实现队列
- 《剑指offer》-用两个栈实现队列
- RabbitMQ学习记录(一)
- 社会化评论系统对比
- impala负载均衡
- 运行时机制的原理和运用场景
- Ubuntu JAVA安装与配置
- 《剑指offer》用两个队列实现栈 Java版实现
- 查看Linux服务器内存使用情况
- 关于桥接
- React组件生命周期
- QT socket 服务器端代码
- Linux常见命令
- 深入理解Redis主键失效原理及实现机制
- 二叉树的镜像
- 解决一则ORA-00600[kcratr_nab_less_than_odr]的故障