1. /*************************************************************************
  2. *
  3. * A generic queue, implemented using a *circular* linked list.
  4. * (Exercise 1.3.29)
  5. *
  6. * % java Ex_1_3_29 < tobe.txt
  7. * to be or not to be (2 left on queue)
  8. *
  9. *************************************************************************/
  10.  
  11. import java.util.Iterator;
  12. import java.util.NoSuchElementException;
  13.  
  14. public class Ex_1_3_29<Item> implements Iterable<Item> {
  15. private int N;
  16. private Node last;
  17.  
  18. private class Node {
  19. private Item item;
  20. private Node next;
  21. }
  22.  
  23. /**
  24. * Create an empty queue.
  25. */
  26. public Ex_1_3_29() {
  27. last = null;
  28. }
  29.  
  30. /**
  31. * Is the queue empty?
  32. */
  33. public boolean isEmpty() {
  34. return last == null;
  35. }
  36.  
  37. /**
  38. * Return the number of items in the queue.
  39. */
  40. public int size() {
  41. return N;
  42. }
  43.  
  44. /**
  45. * Return the item least recently added to the queue.
  46. * Throw an exception if the queue is empty.
  47. */
  48. public Item peek() {
  49. if (isEmpty()) throw new RuntimeException("Queue underflow");
  50. return last.next.item;
  51. }
  52.  
  53. /**
  54. * Add the item to the queue.
  55. */
  56. public void enqueue(Item item) {
  57. Node x = new Node();
  58. x.item = item;
  59. if (isEmpty())
  60. x.next = x;
  61. else
  62. {
  63. x.next = last.next;
  64. last.next = x;
  65. }
  66. last = x;
  67. N++;
  68. }
  69.  
  70. /**
  71. * Remove and return the item on the queue least recently added.
  72. * Throw an exception if the queue is empty.
  73. */
  74. public Item dequeue() {
  75. if (isEmpty()) throw new RuntimeException("Queue underflow");
  76. Item item = last.next.item;
  77. if (last.next == last)
  78. last = null;
  79. else
  80. last.next = last.next.next;
  81. N--;
  82. return item;
  83. }
  84.  
  85. /**
  86. * Return string representation.
  87. */
  88. public String toString() {
  89. StringBuilder s = new StringBuilder();
  90. for (Item item : this)
  91. s.append(item + " ");
  92. return s.toString();
  93. }
  94.  
  95. /**
  96. * Return an iterator that iterates over the items on the queue in FIFO order.
  97. */
  98. public Iterator<Item> iterator() {
  99. return new ListIterator();
  100. }
  101.  
  102. private class ListIterator implements Iterator<Item> {
  103. private int n = N;
  104. private Node current = last;
  105.  
  106. public boolean hasNext() { return n > 0; }
  107. public void remove() { throw new UnsupportedOperationException(); }
  108.  
  109. public Item next() {
  110. if (!hasNext()) throw new NoSuchElementException();
  111. Item item = current.next.item;
  112. current = current.next;
  113. n--;
  114. return item;
  115. }
  116. }
  117.  
  118. public static void main(String[] args) {
  119. Ex_1_3_29<String> q = new Ex_1_3_29<String>();
  120. while (!StdIn.isEmpty()) {
  121. String item = StdIn.readString();
  122. if (!item.equals("-")) q.enqueue(item);
  123. else if (!q.isEmpty()) StdOut.print(q.dequeue() + " ");
  124. }
  125. StdOut.println("(" + q.size() + " left on queue: [ " + q + "])");
  126. }
  127. }

算法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. LeetCode OJ:Construct Binary Tree from Inorder and Postorder Traversal(从中序以及后序遍历结果中构造二叉树)

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  2. 使用visio 2010建立sql server数据模型——手动画、利用逆向工程

    基础数据库这个词不在新鲜,老早就提出了.咱们从出生,个人信息就被放到一个基本信息库中了,在全国各地,通过身份证号就能知道你的基本信息.最近米老师 下发了一个任务,让我们开发几个小项目,考试系统.选修课 ...

  3. unity3d IO操作

             前几天有个朋友问我为什么在IOS平台中可以正常的读写文件可是在Android平台中就无法正常的读写.当时因为在上班所以我没时间来帮他解决,晚上回家后我就拿起安卓手机真机调试很快就定位 ...

  4. Laser

    Petya is the most responsible worker in the Research Institute. So he was asked to make a very impor ...

  5. angular.run 妙用

    **1.浏览器判断**在angular做微信应用的时候,有时候我们也想把相同一份代码运行在非微信的浏览器上,这时候我们可以在angular的run上写点东西实现~例如asw.run函数里执行定义一个$ ...

  6. Python 函数之装饰器

    1.函数 #### 第一波 #### def foo(): print 'foo' foo #表示是函数 foo() #表示执行foo函数 #### 第二波 #### def foo(): print ...

  7. kindeditro.js乱码问题

    kindeditor.js是用于显示新建邮件时的菜单栏的一个插件,比较好用,但是在引入的时候会出现乱码问题,主要有几个方面原因. 1.编码方式不对,要设置成utf8. <script chars ...

  8. error: cast from ‘char*’ to ‘int’ loses precision

    程序: char* addrCom; addrCom= ......//赋值 == (int)addrCom) //导致编译出错 { ...... } 编译时出现错误: error: cast fro ...

  9. MD5加密 及获得密码盐

    MD5加密 及获得密码盐 using System; using System.Collections.Generic; using System.Configuration; using Syste ...

  10. (转)基于PHP——简单的WSDL的创建(WSDL篇)

    本文转载自:http://blog.csdn.net/rrr4578/article/details/24451943 1.建立WSDL文件     建立WSDL的工具很多,eclipse.zends ...