每天一道算法题——用栈实现队列
2017-10-31 10:28
393 查看
这道题也是我面试中的一道题,但是这道题答的也不是很好,今天就来总结这道题所用的算法吧。首先,用两个栈实现队列:
由于之前对数据结构和算法不是很了解,只是知道栈是LIFO,队列是FIFO,当被问到的时候思考了半天,想出来一个不是很巧妙的算法:设置两个栈分别为stack1,stack2,入队操作我想的就是将数据压入到stack1中,而出队操作就是将pop出stack1中所有元素,并将它们push到stack二中,这样顺序就倒回来了。然后弹出栈顶元素,再将stack2中的元素出栈,压入到stack1中。这样来回入队,出对比较繁琐。
优化算法:
入队在stack1中进行,出队在stack2中进行。
入队:直接把元素压入stack1中。
出队:如果stack2不为空,每次都弹出栈顶元素;如果stack2为空,就将stack1中所有元素倒入stack2中,然后弹出stack2中的栈顶元素。若两个栈都为空,则无法出队
下面是代码实现过程:
出入栈都在queue1完成,而将queue2作为一个中转。
入栈:直接压入queue1中
出栈:将queue1除最后一个元素外的 所有元素倒入queue1中,再将queue1中的元素出队,再把queue2中的元素到会queue1中。
优化方案:
入栈:那个队列不为空,将八院所入队到哪个队列,如果都为空,就随机选择一个。
出栈:把不为空的队列中除最后一个元素外的所有元素移动到另一个队列中,然后出队最后一个元素。
代码实现:
由于之前对数据结构和算法不是很了解,只是知道栈是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(); } }
相关文章推荐
- 每天一道算法题——用两个栈实现队列和用两个队列实现栈
- 【每天学点算法题10.20】用两个栈实现队列
- 每天一道算法题(一) (动态规划算法)背包问题Java实现
- 每天一道算法题(四) (动态规划算法)01背包问题Java实现
- 每天一个小算法(6)---- 通过两个栈实现一个队列
- 用栈实现队列(每日一道算法题)
- java实现最大公约数和最小公倍数(每天一道算法题)
- 每天学习一算法系列(31)(实现一个队列,队列的应用场景为:一个生产者线程将int 类型的数入列,一个消费者线程将int 类型的数出列)
- 每天一道算法题(六)深度优先算法(DFS)学习及Java实现
- 【每天学点算法题10.21】用两个队列实现栈
- 每天一个算法之两栈实现队列
- 每天一道算法题(11)——栈的push、pop 序列
- 【每天一道算法题】Lucky String
- [算法拾遗]栈和队列的实现
- 每天一道算法题14 从上往下遍历二元树
- 一道微软公司的面试题目的算法实现
- 【每天一道算法题】恍如隔世
- 每天一道算法题(23)——约瑟夫环问题
- 每天一道算法题19 寻找丑数
- 每天一道算法题3 求子数组的最大和