/*************************************************************************
*
* A generic queue, implemented using a *circular* linked list.
* (Exercise 1.3.29)
*
* % java Ex_1_3_29 < tobe.txt
* to be or not to be (2 left on queue)
*
*************************************************************************/ import java.util.Iterator;
import java.util.NoSuchElementException; public class Ex_1_3_29<Item> implements Iterable<Item> {
private int N;
private Node last; private class Node {
private Item item;
private Node next;
} /**
* Create an empty queue.
*/
public Ex_1_3_29() {
last = null;
} /**
* Is the queue empty?
*/
public boolean isEmpty() {
return last == null;
} /**
* Return the number of items in the queue.
*/
public int size() {
return N;
} /**
* Return the item least recently added to the queue.
* Throw an exception if the queue is empty.
*/
public Item peek() {
if (isEmpty()) throw new RuntimeException("Queue underflow");
return last.next.item;
} /**
* Add the item to the queue.
*/
public void enqueue(Item item) {
Node x = new Node();
x.item = item;
if (isEmpty())
x.next = x;
else
{
x.next = last.next;
last.next = x;
}
last = x;
N++;
} /**
* Remove and return the item on the queue least recently added.
* Throw an exception if the queue is empty.
*/
public Item dequeue() {
if (isEmpty()) throw new RuntimeException("Queue underflow");
Item item = last.next.item;
if (last.next == last)
last = null;
else
last.next = last.next.next;
N--;
return item;
} /**
* Return string representation.
*/
public String toString() {
StringBuilder s = new StringBuilder();
for (Item item : this)
s.append(item + " ");
return s.toString();
} /**
* Return an iterator that iterates over the items on the queue in FIFO order.
*/
public Iterator<Item> iterator() {
return new ListIterator();
} private class ListIterator implements Iterator<Item> {
private int n = N;
private Node current = last; public boolean hasNext() { return n > 0; }
public void remove() { throw new UnsupportedOperationException(); } public Item next() {
if (!hasNext()) throw new NoSuchElementException();
Item item = current.next.item;
current = current.next;
n--;
return item;
}
} public static void main(String[] args) {
Ex_1_3_29<String> q = new Ex_1_3_29<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: [ " + q + "])");
}
}

算法Sedgewick第四版-第1章基础-015一stack只保留last指针的更多相关文章

  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. c++primer 第l六章编程练习答案

    6.11.1 #include<iostream> #include<cctype> int main() { using namespace std; char ch; ci ...

  2. ps6-工具的基础使用

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

  3. XSLT 教程

    应用自http://www.w3school.com.cn/xsl/index.asp XSL 指扩展样式表语言(EXtensible Stylesheet Language). 万维网联盟开始发展 ...

  4. 关于Intellij IDEA导入jdk出现异常

    目前JDK出现的情况如下: 这里JavaJDK是有问题的,因为只有jre而没有JDK.正确做法是重新安装新的JDK. 正常的JDK下的目录是这样的: 选择jdk开头的文件夹就可以了.

  5. Windows命令查看文件的MD5/SHA1/SHA256

    certutil -hashfile "D:\Tools\Microsoft\SqlServer\2016\ct_sql_server_2016_enterprise_x64_dvd_869 ...

  6. 机器学习:模型泛化(岭回归:Ridge Regression)

    一.基础理解 模型正则化(Regularization) # 有多种操作方差,岭回归只是其中一种方式: 功能:通过限制超参数大小,解决过拟合或者模型含有的巨大的方差误差的问题: 影响拟合曲线的两个因子 ...

  7. JS:Window

    ylbtech-JS:Window 1.返回顶部 1.happy.js ; (function () { var happyUi = { initHappy: function (type) { ut ...

  8. [python] itertools库学习

    最近做 cyber-dojo上的题,好几道都要用到排列组合.一开始我还老老实实自己写算法.后来一想,不对呀!python有那么多的库,为啥不用呢? 于是搜了下,发现这个:itertools 使用 he ...

  9. eclipse中删除tomcat server 导致不能重新创建该server

    定位到:workspace\.metadata\.plugins\org.eclipse.core.runtime\.settings 1 打开org.eclipse.jst.server.tomca ...

  10. iOS类目、延展和协议

    类目:为已知的类增加新的方法:注意:类目里面只能写方法,不能写声明和属性,所以,类目不能作为接口来用 1.类目无法向已有类中添加实例变量.2.如果类目中的方法和已有类中的方法名称冲突时,类目中的方法优 ...