用两个栈实现队列&&用两个队列实现一个栈
来源:互联网 发布:ie60和ie80知乎 编辑:程序博客网 时间:2024/05/06 18:36
一.两个栈实现一个队列
思路:所有元素进stack1,然后全部出stack1并进入stack2.实现队列的先进先出即:若stack2非空,我们需要的恰好在栈顶,出栈;若要给队列添加元素,即先进sack1,要出队时,若stack2不为空就出栈,为空时就把stack1全部进栈到stack2。
ps:图片原创于剑指offer,来自网络
import java.util.ArrayList; import java.util.List; import java.util.Stack; /* * Q 57 用两个栈实现队列 */ public class QueueImplementByTwoStacks { private Stack<Integer> stack1; private Stack<Integer> stack2; QueueImplementByTwoStacks(){ stack1=new Stack<Integer>(); stack2=new Stack<Integer>(); } public Integer poll(){ Integer re=null; if(!stack2.empty()){ re=stack2.pop(); }else{ while(!stack1.empty()){//move to stack2 to make stack1 have only one element.Then pop this element. re=stack1.pop(); stack2.push(re); } if(!stack2.empty()){ re=stack2.pop(); } } return re; } public Integer offer(int o){ stack1.push(o); return o; } public static void main(String[] args) { QueueImplementByTwoStacks queue=new QueueImplementByTwoStacks(); List<Integer> re=new ArrayList<Integer>(); queue.offer(1); queue.offer(2); queue.offer(3); re.add(queue.poll()); queue.offer(4); re.add(queue.poll()); queue.offer(5); re.add(queue.poll()); re.add(queue.poll()); re.add(queue.poll()); System.out.println(re.toString()); } }
二.两个队列实现一个栈
思路:假设有两个队列Q1和Q2,当二者都为空时,入栈操作可以用入队操作来模拟,可以随便选一个空队列,假设选Q1进行入栈操作,现在假设a,b,c依次入栈了(即依次进入队列Q1),这时如果想模拟出栈操作,则需要将c出栈,因为在栈顶,这时候可以考虑用空队列Q2,将a,b依次从Q1中出队,而后进入队列Q2,将Q1的最后一个元素c出队即可,此时Q1变为了空队列,Q2中有两个元素,队头元素为a,队尾元素为b,接下来如果再执行入栈操作,则需要将元素进入到Q1和Q2中的非空队列,即进入Q2队列,出栈的话,就跟前面的一样,将Q2除最后一个元素外全部出队,并依次进入队列Q1,再将Q2的最后一个元素出队即可。
ps:图片原创于剑指offer,来自网络
import java.util.LinkedList; /* * Q 57 用两个队列实现一个栈 */ public class StackImplementByTwoQueues { //use 'queue1' and 'queue2' as a queue.That means only use the method 'addLast' and 'removeFirst'. private LinkedList<Integer> queue1; private LinkedList<Integer> queue2; StackImplementByTwoQueues(){ queue1=new LinkedList<Integer>(); queue2=new LinkedList<Integer>(); } public Integer pop(){ Integer re=null; if(queue1.size()==0&&queue2.size()==0){ return null; } if(queue2.size()==0){ while(queue1.size()>0){ re=queue1.removeFirst(); if(queue1.size()!=0){//do not add the last element of queue1 to queue2 queue2.addLast(re); } } }else if (queue1.size()==0){ while(queue2.size()>0){ re=queue2.removeFirst(); if(queue2.size()!=0){//do not add the last element of queue2 to queue1 queue1.addLast(re); } } } return re; } public Integer push(Integer o){ if(queue1.size()==0&&queue2.size()==0){ queue1.addLast(o);//queue2.addLast(o); is also ok } if(queue1.size()!=0){ queue1.addLast(o); }else if(queue2.size()!=0){ queue2.addLast(o); } return o; } public static void main(String[] args) { StackImplementByTwoQueues stack=new StackImplementByTwoQueues(); int tmp=0; stack.push(1); stack.push(2); stack.push(3); tmp=stack.pop(); System.out.println(tmp);//3 stack.push(4); tmp=stack.pop(); System.out.println(tmp);//4 tmp=stack.pop(); System.out.println(tmp);//2 stack.push(5); stack.push(6); tmp=stack.pop(); System.out.println(tmp);//6 tmp=stack.pop(); System.out.println(tmp);//5 tmp=stack.pop(); System.out.println(tmp);//1 } }
0 0
- 用两个栈实现队列 & 两个队列实现一个栈
- 两个栈实现队列&&用两个队列实现一个栈
- 用两个栈实现一个队列
- 用两个栈实现一个队列
- 用两个栈实现一个队列
- 用两个队列实现一个栈
- 用两个栈实现一个队列功能
- 用两个栈实现一个队列
- 用两个队列实现一个栈
- 用两个栈实现一个队列
- 用两个队列实现一个栈
- 用两个栈来实现一个队列
- 用两个栈实现一个队列功能
- 用两个队列实现一个栈
- 用两个队列实现一个栈
- 用两个栈实现一个队列
- 用两个栈实现一个队列
- 用两个栈实现一个队列
- 人生如果是十分…
- JAVA正则表达式 Pattern和Matcher
- nyoj--27--水池数目(dfs)
- PLSQL连接远程Oracle
- 小米Pad退出开发者模式
- 用两个栈实现队列&&用两个队列实现一个栈
- 架构师于小波:魅族实时消息推送架构
- NYOJ 266 字符串逆序输出
- 如何在Android jni 的c/c++中打Log
- Spring中@Transactional事务回滚(含实例详细讲解,附源码)
- 就这样真的赌了
- Android的TextView使用Html来处理图片显示、字体样式、超链接等
- C#如何读写和创建INI文件
- Happy Number 判断一个整数的各个位数的平方的和到最后是不是1