用两个栈实现队列
来源:互联网 发布:金刚1024控台编程 编辑:程序博客网 时间:2024/06/06 00:54
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
public class Solution9 { static Stack<Integer> a = new Stack<Integer>(); static Stack<Integer> b = new Stack<Integer>(); public void push(int node){ while (!a.isEmpty()){ a.push(node); } } public int pop(){ while (!a.isEmpty()){ b.push(a.pop()); } while (!b.isEmpty()){ a.push(b.pop()); } int c = b.pop(); return c; } public static void main(String args[]){ int[] arry = {1,2,3,4,5}; System.out.println("输入是:"); for (int j=0;j<arry.length;j++){ System.out.print("\t"+arry[j]); } System.out.println("\n输出是:"); for (int i=0;i<arry.length;i++){ b.push(arry[i]); System.out.print("\t"+b.pop()); } }}
0 0
- 用两个栈实现队列 & 用两个队列实现栈
- 用两个队列实现栈&用两个栈实现队列
- 用两个栈实现队列 & 两个队列实现一个栈
- 两个栈实现队列&&用两个队列实现一个栈
- 用两个栈实现队列 & 两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- 二叉树的前序遍历
- servlet生命周期
- Keil综合(01)_一些常见文件类型的作用和功能说明
- java获取当前时间(任意格式)
- VC++6.0与VS2010的区别
- 用两个栈实现队列
- 如何提高服务器并发处理能力
- BZOJ 2142: 礼物
- XML实现AOP
- 水模拟
- 特征匹配中的欧氏距离
- ubuntu16.04——WingIDE安装
- OpenStack tempest安装与运行测试
- ZOJ 3212 K-Nice(思维)