算法Sedgewick第四版-第1章基础-022一QueueWithTwoStacks
/******************************************************************************
* Compilation: javac QueueWithTwoStacks.java
* Execution: java QueueWithTwoStacks < input.txt
* Dependencies: StdIn.java StdOut.java
* Data files: http://algs4.cs.princeton.edu/13stacks/tobe.txt
*
* A generic queue, implemented using two stacks.
*
* % java QueueWithTwoStacks < tobe.txt
* to be or not to be (2 left on queue)
*
******************************************************************************/ import java.util.NoSuchElementException; public class QueueWithTwoStacks<Item> {
private Stack<Item> stack1; // back of queue
private Stack<Item> stack2; // front of queue // create an empty queue
public QueueWithTwoStacks() {
stack1 = new Stack<Item>();
stack2 = new Stack<Item>();
} // move all items from stack1 to stack2
private void moveStack1ToStack2() {
while (!stack1.isEmpty())
stack2.push(stack1.pop());
} // is the queue empty?
public boolean isEmpty() {
return stack1.isEmpty() && stack2.isEmpty();
} // return the number of items in the queue.
public int size() {
return stack1.size() + stack2.size();
} // return the item least recently added to the queue.
public Item peek() {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
if (stack2.isEmpty()) moveStack1ToStack2();
return stack2.peek();
} // add the item to the queue
public void enqueue(Item item) {
stack1.push(item);
} // remove and return the item on the queue least recently added
public Item dequeue() {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
if (stack2.isEmpty()) moveStack1ToStack2();
return stack2.pop();
} // a test client
public static void main(String[] args) {
QueueWithTwoStacks<String> q = new QueueWithTwoStacks<String>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-")) q.enqueue(item);
else if (!q.isEmpty()) StdOut.print(q.dequeue() + " ");
}
StdOut.println("(" + q.size() + " left on queue)");
}
}
算法Sedgewick第四版-第1章基础-022一QueueWithTwoStacks的更多相关文章
- 算法Sedgewick第四版-第1章基础-001递归
一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)
一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版
package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)
一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)
一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...
- 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的
1. package algorithms.stacks13; /******************************************************************* ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法
1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...
随机推荐
- 分布式_理论_03_2PC
一.前言 五.参考资料 1.分布式理论(三)—— 一致性协议之 2PC 2.分布式理论(三) - 2PC协议
- Ajax做无刷新分页
1.主页面代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- 【ASP.NET Web API2】初识Web API
Web Api 是什么? MSDN:ASP.NET Web API 是一种框架,用于轻松构建可以访问多种客户端(包括浏览器和移动设备)的 HTTP 服务 百度百科:Web API是网络应用程序接口. ...
- BZOJ3624:[APIO2008]免费道路
浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...
- Spring Boot基本配置
本文参考javaEE开发的颠覆者SpringBoot实战第一版 基本配置 入口类和@SpringBootApplication Spring Boot通常有一个名为*Application的入口类,且 ...
- Mac上制作Centos7系统U盘安装盘
Centos7 下载地址: http://101.110.118.47/isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-DVD- ...
- apache配置详解 apache安装路径
http://www.linuxidc.com/Linux/2015-02/113921.htm 不同apache的安装方式 的安装目录示例 http://www.121down.com/articl ...
- [转载]rmmod: can't change directory to '/lib/modules': No such file or directory
转载网址:http://blog.csdn.net/chengwen816/article/details/8781096 在我新移植的kernel(3.4.2)和yaffs2文件中,加载新编译的内核 ...
- php用zendstudio建立wsdl
首先,新建的时候要选择soap,然后deocument和rpc都可以. 类和方法的页面: <?php //发货接口 class test{ function send_do_delivery($ ...
- Java-API:java.text.SimpleDateFormat
ylbtech-Java-API:java.text.SimpleDateFormat 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 0. https://do ...