面试题(九)用两个栈实现队列 标签: 面试题
2018-02-09 23:05
211 查看
题目:
用两个栈实现一个队列,需要实现队列的在尾部插入节点和在头部删除节点的功能。队列的声明如下:
思路:
通过列举具体的例子来分析两个栈实现的队列的插入和删除元素的过程(将stack1用作插入元素的栈,stack2用作删除元素的栈),可以总结发现:
(1)若stack2不为空,则stack2的栈顶元素即为最先进入队列的元素,将其直接弹出。
(2)若stack2为空,则stack1底部的元素即为最先进入队列的元素,将stack1中的元素全部弹出到stack2中,则位于stack2栈顶的元素即为最先入队的元素,将其直接弹出。
代码(已在牛客网AC):
扩展题目:
用两个队列实现栈。
思路(举例说明):
将元素a、b、c插入queue1,此时若要删除c,则应该将a、b移动到queue2,再从queue1中删除c。此时queue2中从头至尾元素为a、b。此时若要删除b,则应该将a移至queue1,再从queue2中删除b。此时若要向栈中插入元素,就可以直接入队queue1,因为此时queue1不为空。后续的插入和删除操作重复上述过程。
代码(含单元测试):
用两个栈实现一个队列,需要实现队列的在尾部插入节点和在头部删除节点的功能。队列的声明如下:
public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { } public int pop() { } }
思路:
通过列举具体的例子来分析两个栈实现的队列的插入和删除元素的过程(将stack1用作插入元素的栈,stack2用作删除元素的栈),可以总结发现:
(1)若stack2不为空,则stack2的栈顶元素即为最先进入队列的元素,将其直接弹出。
(2)若stack2为空,则stack1底部的元素即为最先进入队列的元素,将stack1中的元素全部弹出到stack2中,则位于stack2栈顶的元素即为最先入队的元素,将其直接弹出。
代码(已在牛客网AC):
public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { stack1.push(node); } public int pop() { if(stack2.empty()){ while(!stack1.empty()) stack2.push(stack1.pop()); } return stack2.pop(); } }
扩展题目:
用两个队列实现栈。
思路(举例说明):
将元素a、b、c插入queue1,此时若要删除c,则应该将a、b移动到queue2,再从queue1中删除c。此时queue2中从头至尾元素为a、b。此时若要删除b,则应该将a移至queue1,再从queue2中删除b。此时若要向栈中插入元素,就可以直接入队queue1,因为此时queue1不为空。后续的插入和删除操作重复上述过程。
代码(含单元测试):
import java.util.LinkedList; public class TwoQueueToStack{ private LinkedList<Integer> queue1 = new LinkedList<>(); private LinkedList<Integer> queue2 = new LinkedList<>(); public static void main(String[] args){ TwoQueueToStack stack = new TwoQueueToStack(); stack.test1(); stack.test2(); stack.test3(); stack.test4(); } public void push(int node){ if(queue1.size() != 0) queue1.addLast(node); else if(queue2.size() != 0) queue2.addLast(node); else queue1.addLast(node); } public int pop(){ if(queue1.size() != 0){ while(queue1.size() != 1) queue2.addLast(queue1.pollFirst()); return queue1.pollFirst(); } else if(queue2.size() != 0){ while(queue2.size() != 1) queue1.addLast(queue2.pollFirst()); return queue2.pollFirst(); } else return -1; } public boolean isEmpty(){ if(queue1.size() == 0 && queue2.size() == 0) return true; return false; } public void test1(){ push(1); push(2); push(3); push(4); push(5); while(!isEmpty()) System.out.printf("%d ", pop()); System.out.println(); } public void test2(){ push(1); push(2); push(3); System.out.printf("%d ", pop()); System.out.printf("%d ", pop()); System.out.printf("%d ", pop()); push(4); push(5); System.out.printf("%d ", pop()); System.out.printf("%d ", pop()); System.out.println(); } public void test3(){ push(1); push(2); push(3); System.out.printf("%d ", pop()); push(4); push(5); System.out.printf("%d ", pop()); System.out.printf("%d ", pop()); System.out.printf("%d ", pop()); System.out.printf("%d ", pop()); System.out.println(); } public void test4(){ push(1); push(2); push(3); System.out.printf("%d ", pop()); System.out.printf("%d ", pop()); System.out.printf("%d ", pop()); System.out.printf("%d ", pop()); System.out.printf("%d ", pop()); System.out.println(); } }
相关文章推荐
- 剑指offer--面试题7: 用两个栈实现队列
- 面试题7:用两个栈实现一个队列
- 面试题整理-两个栈实现队列
- 【剑指Offer】面试题7:用两个栈实现队列
- 【练习笔记】剑指offer-面试题7 :用两个栈实现队列
- 剑指Offer——面试题7:用两个栈实现队列
- 剑指offer面试题7:用两个栈实现队列
- 剑指offer 面试题7 用两个栈实现队列
- 面试题13 - 用两个栈实现队列 【栈】
- 【数据结构】栈面试题--两个栈实现一个队列
- 剑指offer面试题7——用两个栈实现队列
- C++面试题:两个队列实现一个栈
- 面试题7—用两个栈实现队列
- 剑指offer面试题之用两个栈实现队列
- 剑指offer - 面试题7:用两个栈实现队列
- 剑指Offer系列-面试题7:用两个栈实现队列
- 面试题7:用两个栈实现队列
- 高频面试题之三种方法实现两个栈实现一个队列
- 面试题07_用两个栈实现队列——剑指offer系列
- 剑指offer--面试题7:用两个栈实现队列--Java实现