两个stack实现一个queue
2024-08-31 03:30:31
package com.hzins.suanfa; import java.util.Stack;
/**
* 两个stack实现一个queue
* @author Administrator
*
*/
public class TwoStackToQueue {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
public TwoStackToQueue(){
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
}
/**
* 入队
* @param value
*/
public void push(int value){
stack1.push(value);
}
/**
* 出队
*/
public int pop(){
while(!stack2.isEmpty()){
return stack2.pop();
}
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
return stack2.pop();
}
public static void main(String[] args) {
// Stack<Integer> stack1 = new Stack<Integer>();
// stack1.push(1);
// stack1.push(2);
// stack1.push(3);
// stack1.push(4);
// System.out.println(stack1.size());
// System.out.println(stack1.pop());
// System.out.println(stack1.pop());
// System.out.println(stack1.pop());
// System.out.println(stack1.pop());
TwoStackToQueue queue = new TwoStackToQueue();
queue.push(1);
queue.push(2);
queue.push(3);
System.out.println(queue.pop());
System.out.println(queue.pop());
queue.push(4);
System.out.println(queue.pop());
}
}
最新文章
- springmvc+bootstrap+jquerymobile完整搭建案例(提供下载地址)
- JQuery EasyUI DataGrid列表所见所得随意导出excel
- flume 读取tcp写到hdfs
- JVM探索之——内存管理(二)
- 2015 Multi-University Training Contest 2 1006 Friends
- 【SVN】删除文件/文件夹 svn: E205007: Could not use external editor to fetch log message
- JBoss 系列九十九:Rest WebService jBPM 6 集成演示样例
- javaio学习笔记-字符流类(2)
- C# 动态对象(dynamic)的用法
- leaflet 利用ajax 将前端地图上的数据post到后台
- [转]Understanding OpenStack Authentication: Keystone PKI
- 爬取B站视频
- windows下vbs脚本隐藏控制台
- JS简单验证
- [转] HTML5之FileReader的使用
- Javascript数据模型
- [原]git的使用(一)---建立本地仓库、add和commit、status和git diff、版本回退使用git reset
- golang---interface结合reflect的泛型应用
- 淘宝开放平台TOP SDK调用对接淘宝或天猫
- MySQL存储过程中的3种循环【转载】