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());
}
}

最新文章

  1. springmvc+bootstrap+jquerymobile完整搭建案例(提供下载地址)
  2. JQuery EasyUI DataGrid列表所见所得随意导出excel
  3. flume 读取tcp写到hdfs
  4. JVM探索之——内存管理(二)
  5. 2015 Multi-University Training Contest 2 1006 Friends
  6. 【SVN】删除文件/文件夹 svn: E205007: Could not use external editor to fetch log message
  7. JBoss 系列九十九:Rest WebService jBPM 6 集成演示样例
  8. javaio学习笔记-字符流类(2)
  9. C# 动态对象(dynamic)的用法
  10. leaflet 利用ajax 将前端地图上的数据post到后台
  11. [转]Understanding OpenStack Authentication: Keystone PKI
  12. 爬取B站视频
  13. windows下vbs脚本隐藏控制台
  14. JS简单验证
  15. [转] HTML5之FileReader的使用
  16. Javascript数据模型
  17. [原]git的使用(一)---建立本地仓库、add和commit、status和git diff、版本回退使用git reset
  18. golang---interface结合reflect的泛型应用
  19. 淘宝开放平台TOP SDK调用对接淘宝或天猫
  20. MySQL存储过程中的3种循环【转载】

热门文章

  1. 04 Memcached过期机制与删除机制
  2. FMM和BMM的python代码实现
  3. ICloneable接口 Clone 深拷贝 浅拷贝
  4. mysql-5.1.73多实例安装启动
  5. python pymysql安装
  6. Android 与H5之间的js交互
  7. sqlserver删除所有表
  8. EventLoop(netty源码死磕4)
  9. linux c编程:非阻塞I/O
  10. mysql 导入数据是报错:2006 - MySQL server has gone away