剑指offer-面试题07-衍生题目:用两个队列实现栈
来源:互联网 发布:黄河商品交易软件 编辑:程序博客网 时间:2024/06/07 04:57
package case07_StackWithTwoQueues;/** *题目:用两个队列实现栈,并且实现栈的pop,push,empty,top(获取栈顶元素,但是不删除) * @author WangSai * */import java.util.ArrayDeque;public class StackWithTwoQueues<T> {// 两个队列,在模拟实现栈的时候,始终保持其中一个队列为空。private ArrayDeque<T> queue1;private ArrayDeque<T> queue2;public StackWithTwoQueues() {queue1 = new ArrayDeque<>();queue2 = new ArrayDeque<>();}// 从from队列中取出元素,并且送入队列to中。在队列from中保留最后一个元素。private void move(ArrayDeque<T> from, ArrayDeque<T> to) {if (!isEmpty()) {while (from.size() > 1) {// 从from队列中取出元素,并且送入队列to中。在队列from中保留最后一个元素。to.offer(from.poll());}}}// 模拟入栈操作,queque1始终作为public void push(T t) {queue1.offer(t);}// 模拟出栈操作,弹出并返回栈顶元素。public T pop() {T top = null;if (!isEmpty()) {if (!queue1.isEmpty()) {move(queue1, queue2);top = queue1.poll();} else {move(queue2, queue1);top = queue2.poll();}}return top;}// 模拟栈的top操作public T top() {T top = null;if (!isEmpty()) {if(!queue1.isEmpty()){move(queue1,queue2);top = queue1.poll();queue2.offer(top);}else{move(queue2,queue1);top = queue2.poll();queue1.offer(top);}}return top;}// 实现栈为空功能public boolean isEmpty() {return queue1.isEmpty() && queue2.isEmpty();}public static void main(String[] args) {StackWithTwoQueues<String> myStack = new StackWithTwoQueues<>();myStack.push("1st e");myStack.push("2nd e");myStack.push("3rd e");System.out.println("第1次:myStack.pop():"+myStack.pop());System.out.println("第3次:myStack.pop():"+myStack.pop());System.out.println("myStack.top():"+myStack.top());System.out.println("第2次:myStack.pop():"+myStack.pop());System.out.println("第4次:myStack.pop():"+myStack.pop());}}
0 0
- 剑指offer-面试题07-衍生题目:用两个队列实现栈
- [剑指offer][面试题07]用两个栈实现队列
- 剑指offer-面试题07-用两个栈实现队列
- 【剑指offer】面试题07:用两个栈实现队列
- 剑指offer-->面试题7 用两个栈实现队列
- 《剑指Offer》面试题-用两个栈实现队列
- 《剑指offer》面试题七 用两个栈实现队列
- 【剑指offer】面试题7:用两个栈实现队列
- 剑指Offer:面试题7 用两个栈实现队列
- 《剑指Offer》面试题7:用两个栈实现队列
- 【剑指offer】 面试题7: 用两个栈实现队列
- 【剑指offer】面试题7:用两个栈实现队列
- 《剑指Offer》面试题:用两个队列实现一个栈
- 剑指offer-面试题7:用两个栈实现队列
- 剑指offer面试题之用两个栈实现队列
- 剑指offer面试题 用两个栈实现队列
- 剑指Offer----面试题七:用两个栈实现队列
- 剑指offer---面试题7 用两个栈实现队列
- 【Spring实战】----Spring事务管理配置解析
- Spring事务之二(JDBC连接数据库的四种方式)
- tranwrd函数用法详解
- DedeCMS V5.7 SP2 去掉广告方法
- Ubuntu16.04下vim的tab缩进设置为4个空格
- 剑指offer-面试题07-衍生题目:用两个队列实现栈
- react vr 中文网地址:www.vr-react.com
- 在计算机中安装Manjaro
- 第一篇博客---记一次因构造函数给我狠狠的教训
- 设计模式——单例模式
- WebService简单实例
- Hibernate继承映射,数据表的继承
- Retrofit2+RxJava学习小计(一):单文件、多文件上传之填平的坑
- 【IMWeb训练营作业】todo-list