如何两个栈实现队列?两个队列实现栈?

初看此题目,你可能觉得自找苦吃,队列就队列为什么要用栈来实现,工程中总是很典型的应用,直接调API就可以了,不管是什么语言,已经有现成API封装好队列以及堆栈的所有操作。没错,在项目中很少有这样的需要。但是我们是站在学习的角度,并不是画地为牢,难为自己,而是对思维的一种锻炼,对算法的提升么。我们在面试的时候碰到的这类问题还少吗?所以大家就不要拍砖了。现在开始研究此问题。

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


  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值