用两个栈实现队列
来源:互联网 发布:一键手绘图的软件 编辑:程序博客网 时间:2024/06/11 14:59
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型
/** * 解法一:将数据元素压入第一个栈,完成入队操作; * 当第一个栈不为空时, 将第一个栈的所有数据元素出栈并压入第二个栈中,然后弹出第二个栈的栈顶元素,完成出队操作; * 当第二个栈不为空时,将第二个栈的所有数据元素出栈并压入第一个栈中(新压入数据元素在第一个栈中) * */public class Solution { Stack<Integer> stack1 = new Stack<>(); Stack<Integer> stack2 = new Stack<>(); public static void main(String[] args) { Solution solution = new Solution(); solution.push(1); solution.push(2); solution.push(3); System.out.println(solution.pop()); System.out.println(solution.pop()); System.out.println(solution.pop()); } public void push(int node) { // 将数据元素压入第一个栈,完成入队操作 stack1.push(node); } public int pop() { // 当第一个栈不为空时, 将第一个栈的所有数据元素出栈并压入第二个栈中 while(!stack1.isEmpty()) { stack2.push(stack1.pop()); } // 弹出第二个栈的栈顶元素,完成出队操作 int first = stack2.pop(); // 当第二个栈不为空时,将第二个栈的所有数据元素出栈并压入第一个栈中(新压入数据元素在第一个栈中) while(!stack2.isEmpty()) { stack1.push(stack2.pop()); } return first; }}
/** * 解法二:将数据元素压入第一个栈,完成入队操作; * 当第二个栈为空时,将第一个栈中的数据元素弹出并压入第二个栈中; * 当第二个栈不为空时,弹出第二个栈的栈顶元素(新入栈的数据元素暂存于第一个栈中),完成出队操作 * */public class Solution { Stack<Integer> stack1 = new Stack<>(); Stack<Integer> stack2 = new Stack<>(); public static void main(String[] args) { Solution solution = new Solution(); solution.push(1); solution.push(2); solution.push(3); System.out.println(solution.pop()); System.out.println(solution.pop()); System.out.println(solution.pop()); } public void push(int node) { // 将数据元素压入第一个栈,完成入队操作 stack1.push(node); } public int pop() { if (stack1.empty() && stack2.empty()) { throw new RuntimeException("Queue is empty!"); } // 当第二个栈为空时,将第一个栈中的数据元素弹出并压入第二个栈中 if (stack2.empty()) { while (!stack1.empty()) { stack2.push(stack1.pop()); } } // 当第二个栈不为空时,弹出第二个栈的栈顶元素(新入栈的数据元素暂存于第一个栈中),完成出队操作 return stack2.pop(); }}
/** * 解法三:数据元素从第一个栈进入队列,完成入队操作; * 当第二个栈为空时,将第一个栈中的所有数据元素弹出并压入第二个栈,然后弹出第二个栈栈顶元素,完成出队操作; * 当第二个栈不为空时,将第二个栈的栈顶元素弹出并压入第一个栈,然后从第一个栈中弹出并返回该数据元素,完成出队操作 */public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public static void main(String[] args) { Solution solution = new Solution(); solution.push(1); solution.push(2); solution.push(3); System.out.println(solution.pop()); System.out.println(solution.pop()); System.out.println(solution.pop()); } public void push(int node) { // 数据元素从第一个栈进入队列,完成入队操作 stack1.add(node); } public int pop() { // 当第二个栈为空时,将第一个栈中的所有数据元素弹出并压入第二个栈,然后弹出第二个栈栈顶元素,完成出队操作 if (stack2.size() == 0) { while (stack1.size() != 0) { stack2.push(stack1.pop()); } return stack2.pop(); // 当第二个栈不为空时,将第二个栈的栈顶元素弹出并压入第一个栈,然后从第一个栈中弹出并返回该数据元素,完成出队操作 } else { stack1.push(stack2.pop()); return stack1.pop(); } }}
阅读全文
0 0
- 用两个栈实现队列 & 用两个队列实现栈
- 用两个队列实现栈&用两个栈实现队列
- 用两个栈实现队列 & 两个队列实现一个栈
- 两个栈实现队列&&用两个队列实现一个栈
- 用两个栈实现队列 & 两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- sevlet 表单数据
- tf教程(三):Writing a tf listener
- git 上传代码到远程仓库
- deepin15官方源更细特别慢,换阿里云的记录
- 客户端监听器创建和配置
- 用两个栈实现队列
- linux下的eval命令
- Nginx 负载均衡基本配置
- JZ2440 编译 kernel
- iOS Objective-C 数组遍历的性能及原理
- Java语言基础{Java_se(03)}-Fo循环-顺序结构-If语句-switch语句-循环结构-whlie循环-do_whlie循环-控制跳转语句
- 逻辑斯蒂回归(Logistic Regression)
- 简单的双色球预测
- [华为面试题]简单错误记录