每天一道算法题——用栈实现队列

这道题也是我面试中的一道题,但是这道题答的也不是很好,今天就来总结这道题所用的算法吧。首先,用两个栈实现队列:
由于之前对数据结构和算法不是很了解,只是知道栈是LIFO,队列是FIFO,当被问到的时候思考了半天,想出来一个不是很巧妙的算法:设置两个栈分别为stack1,stack2,入队操作我想的就是将数据压入到stack1中,而出队操作就是将pop出stack1中所有元素,并将它们push到stack二中,这样顺序就倒回来了。然后弹出栈顶元素,再将stack2中的元素出栈,压入到stack1中。这样来回入队,出对比较繁琐。
优化算法:
入队在stack1中进行,出队在stack2中进行。
入队:直接把元素压入stack1中。

出队:如果stack2不为空,每次都弹出栈顶元素;如果stack2为空,就将stack1中所有元素倒入stack2中,然后弹出stack2中的栈顶元素。若两个栈都为空,则无法出队


下面是代码实现过程:
package p_13_stack2queue;

import java.util.Stack;

public class Stack2Queue {

	private Stack stack1, stack2;
	private int maxLength = 0;
	
	public Stack2Queue(int capacity) {
		this.maxLength = capacity;
		stack1 = new Stack();
		stack2 = new Stack();
	}
	
	public boolean push(int item) {
		//if(stack1.isEmpty() || stack1.size()>maxLength) {
		if(stack1.size()>maxLength) {
			return false;
		}
		stack1.push(item);
		return true;
	}
	
	public int pop(){
//		if(stack1.isEmpty() && stack2.isEmpty()){
//			return -1;
//		}
		if(stack2.isEmpty()) {
			if(stack1.isEmpty()) {
				return -1;
			}else {
				while(!stack1.isEmpty()){
					stack2.push(stack1.pop());
				}
				return (int) stack2.pop();
			}
		}
		/*if(!stack2.isEmpty()){
			return (int)stack2.pop();
		}else{
			while(!stack1.isEmpty()){
				stack2.push(stack1.pop());
			}
			return (int)stack2.pop();
		}*/
		//stack2.pop();
		return (int)stack2.pop();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Stack2Queue queue =  new Stack2Queue(5);
		queue.push(1);
		queue.push(2);
		System.out.println(queue.pop());
		queue.push(3);
		System.out.println(queue.pop());
		System.out.println(queue.pop());
	}

}
第二个我们来看看怎么使两个队列实现栈:
出入栈都在queue1完成,而将queue2作为一个中转。
入栈:直接压入queue1中
出栈:将queue1除最后一个元素外的 所有元素倒入queue1中,再将queue1中的元素出队,再把queue2中的元素到会queue1中。
优化方案:
入栈:那个队列不为空,将八院所入队到哪个队列,如果都为空,就随机选择一个。
出栈:把不为空的队列中除最后一个元素外的所有元素移动到另一个队列中,然后出队最后一个元素。

代码实现:

package p_13_stack2queue;

import java.util.LinkedList;

public class Queue2Stack {
	private LinkedList queue1, queue2;
	int maxLen = 0;
	
	public Queue2Stack(int capacity){
		maxLen = capacity;
		queue1 = new LinkedList();
		queue2 = new LinkedList();
	}
	
	public boolean push(int item){
		if(size() >= maxLen){
			return false;
		}
		
		if(queue1.isEmpty()){
			queue2.add(item);
		}else {
			queue1.add(item);
		}
		return true;
	}
	public int pop(){
		if(size() == 0){
			throw new IndexOutOfBoundsException("the stack have null");
		}else {
			if(queue2.isEmpty()){
				while(queue1.size() > 1){
					queue2.add(queue1.poll());
				}
				return (int)queue1.poll();
			}else{
				while(queue2.size() > 1){
					queue1.add(queue2.poll());
				}
				return (int)queue2.poll();
			}
		}
		
	}
	
	public int size(){
		return queue1.size()+queue2.size();
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值