首先看如何用两个栈去实现一个队列,栈所具有的操作主要是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());
}
}
}