/******************************************************************************
* 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的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-001递归

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  2. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  3. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

  4. 算法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 ...

  5. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版

    package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...

  6. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)

    一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...

  7. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

  8. 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的

    1. package algorithms.stacks13; /******************************************************************* ...

  9. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

    1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...

随机推荐

  1. Hadoop2.5.2+HA+zookeeper3.4.6详细配置过程

    心血之作,在熟悉hadoop2架构的过程耽误了太长时间,在搭建环境过程遇到一些问题,这些问题一直卡在那儿,不得以解决,耽误了时间.最后,千寻万寻,把问题解决,多谢在过程提供帮助的大侠.这篇文章中,我也 ...

  2. ps6-工具的基础使用

    1.图像的移动与对齐 ctrl+j:复制图层,然后再移动不损坏原来的图像. Ctrl+Z =返回键 Shift+单击最下方图层 选择全部 Alt+鼠标移动 复制并粘贴 2.规则选择工具组 shift键 ...

  3. SQL-表的操作(创建表,删除表,更改列,插入新行,更改行的值,删除表中数据)

    一,操作表及列 1.创建表: CREATE TABLE test (ID int  PRIMARY KEY IDENTITY,Name varchar(20) ) 2.删除表 DROP TABLE t ...

  4. MySQL实战 | 02-MySQL 如何恢复到半个月内任意一秒的状态?

    原文链接:MySQL是如何做到可以恢复到任意一秒状态的? 看到这个题目是不是觉得数据库再也不用担心服务器 crash 了? 那我们需要学习为什么可以这么做?以及如何做? 即为什么可以恢复到任意时间点? ...

  5. bzoj1249: SGU277 HERO 动态凸包

    动态维护凸包面积. //Achen #include<bits/stdc++.h> #define For(i,a,b) for(int i=(a);i<=(b);i++) #def ...

  6. Logstash详解之——input模块

    原文地址 Logstash由三个组件构造成,分别是input.filter以及output.我们可以吧Logstash三个组件的工作流理解为:input收集数据,filter处理数据,output输出 ...

  7. laravel 添加自定义类 全局自定义方法 自定义常量

    添加自定义类 https://blog.csdn.net/suchfool/article/details/38758367 https://blog.csdn.net/liukai6/article ...

  8. java bean Format注解用法

    @NumberFormat(style=Style.NUMBER)    private int number; @DateTimeFormat(pattern="yyyy-MM-dd&qu ...

  9. HDU5475(线段树)

    An easy problem Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  10. Linux驱动 - 多线程

    1.头文件 #include <linux/kthread.h> 2.定义变量 static pid_t thread_id: //线程ID static struct completio ...