如何两个栈实现队列?两个队列实现栈?
2011-11-20 13:05
411 查看
初看此题目,你可能觉得自找苦吃,队列就队列为什么要用栈来实现,工程中总是很典型的应用,直接调API就可以了,不管是什么语言,已经有现成API封装好队列以及堆栈的所有操作。没错,在项目中很少有这样的需要。但是我们是站在学习的角度,并不是画地为牢,难为自己,而是对思维的一种锻炼,对算法的提升么。我们在面试的时候碰到的这类问题还少吗?所以大家就不要拍砖了。现在开始研究此问题。
首先看如何用两个栈去实现一个队列,栈所具有的操作主要是push和pop。也就是面对一个桶,只能在顶上拿元素或放元素,别的地方都是封闭的。而一个队列所具有的特性是offer(尾部入队)和poll(队首出队)。相当于一个两端开口的桶,一端只能放,一端只能取。
而如何将一个只有一端操作的变成两端操作的呢,其实就是用两个一端操作的。就可以完成一个两端操作的。用栈1来专门处理offer(入队操作,在队尾),用栈2来处理poll(出队操作,在队首)。那么栈1和栈2的数据需要完全相反,那么队列两端就完全暴露出来了。自己画个图出来就更明显了。代码如下:
而对于两个队列实现一个栈,想象如果我要把刚放的东西在取出来,对于队列来说,只能是把之前的东西都先出队,才能把刚才的放的东西出队。顺理成章,那就把之前的那些数据先出队到队列2中。两个队列来回交换数据即可。不在赘述。代码如下:
首先看如何用两个栈去实现一个队列,栈所具有的操作主要是push和pop。也就是面对一个桶,只能在顶上拿元素或放元素,别的地方都是封闭的。而一个队列所具有的特性是offer(尾部入队)和poll(队首出队)。相当于一个两端开口的桶,一端只能放,一端只能取。
而如何将一个只有一端操作的变成两端操作的呢,其实就是用两个一端操作的。就可以完成一个两端操作的。用栈1来专门处理offer(入队操作,在队尾),用栈2来处理poll(出队操作,在队首)。那么栈1和栈2的数据需要完全相反,那么队列两端就完全暴露出来了。自己画个图出来就更明显了。代码如下:
import java.util.Stack; public class MyQueue { private Stack<String> stackFirst = new Stack<String>(); private Stack<String> stackSecond = new Stack<String>(); public boolean offer(String str){ if(stackSecond.empty()){ stackFirst.push(str); }else{ while(!stackSecond.empty()){ stackFirst.push(stackSecond.pop()); } stackFirst.push(str); } return true; } public String poll(){ if(!stackSecond.empty()){ return stackSecond.pop(); }else{ while(!stackFirst.empty()){ stackSecond.push(stackFirst.pop()); } return stackSecond.pop(); } } public boolean empty(){ if(stackFirst.empty() && stackSecond.empty()) return true; return false; } public static void main(String[] args){ MyQueue queue = new MyQueue(); queue.offer("hello "); queue.offer("baby "); queue.offer("!"); while(!queue.empty()){ System.out.print(queue.poll()); } } }
而对于两个队列实现一个栈,想象如果我要把刚放的东西在取出来,对于队列来说,只能是把之前的东西都先出队,才能把刚才的放的东西出队。顺理成章,那就把之前的那些数据先出队到队列2中。两个队列来回交换数据即可。不在赘述。代码如下:
import java.util.LinkedList; import java.util.Queue; public class MyStack { private Queue<String> queueFirst = new LinkedList<String>(); private Queue<String> queueSecond = new LinkedList<String>(); public String push(String str){ if(queueSecond.isEmpty()){ queueFirst.offer(str); }else if(queueFirst.isEmpty()){ queueSecond.offer(str); } return str; } public String pop(){ if(!queueFirst.isEmpty()){ while(queueFirst.size() > 1){ queueSecond.offer(queueFirst.poll()); } return queueFirst.poll(); }else if(!queueSecond.isEmpty()){ while(queueSecond.size() > 1){ queueFirst.offer(queueSecond.poll()); } return queueSecond.poll(); } return null; } public boolean empty(){ if(queueFirst.isEmpty() && queueSecond.isEmpty()) return true; return false; } public static void main(String[] args){ MyStack stack = new MyStack(); stack.push("hello"); stack.push("baby"); stack.push("!"); while(!stack.empty()){ System.out.print(stack.pop()); } } }
相关文章推荐
- 用两个栈如何实现队列?
- 如何用两个队列实现一个栈,并分析有关栈操作的运行时间。
- Java:如何利用两个栈实现队列
- 【题目5】如何使用两个栈来实现一个队列
- 用两个栈如何实现队列?
- 用两个栈如何实现队列?
- 用两个栈如何实现队列?
- 用两个栈如何实现队列?
- 用两个栈如何实现队列?
- 用两个栈如何实现队列?
- 如何用两个栈实现一个队列
- 用两个栈如何实现队列?
- 用两个栈如何实现队列?
- 如何用两个栈实现一个队列
- 数据结构:栈和列之如何用两个队列实现一个栈?两个栈实现一个队列?
- 如何用两个栈来实现队列的方法
- 如何用两个栈实现一个队列
- 用两个栈如何实现队列?
- 如何用两个栈实现一个队列,以及用两个队列实现一个栈
- 如何用两个队列实现一个栈,并分析有关栈操作的运行时间。