/*************************************************************************
*
* 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 + "])");
}
}

最新文章

  1. [BZOJ 3637]Query on a tree VI
  2. mysql LAST_INSERT_ID 使用与注意事项
  3. C安全编码--整数理解
  4. eclipse中,把java函数代码折叠/展开
  5. PHP 魔术方法 __construct __destruct (一)
  6. win7 清理系统
  7. zoj 1586 QS Network
  8. Quick Cocos2dx Action相关
  9. 1724: [Usaco2006 Nov]Fence Repair 切割木板
  10. C-static,auto,register,volatile
  11. ArrayList源码阅读笔记(1.8)
  12. 移植QT库的问题:QT_INSTALL/include/QtCore/qatomic_arm.h:131: Error: no such instruction: `swpb %al,
  13. 《探索未知种族之osg类生物》目录
  14. java.lang.StackOverflowError 解决方法
  15. 去掉select的原有样式
  16. qhfl-1 跨域
  17. javascript Date定义和体验
  18. SQL Server —— 查询数据库、表、列等
  19. php 批量导入昨天的数据(别类版的增量备份安案)
  20. SQL Server 2012 自动增长列,值跳跃问题(自增增加1000)

热门文章

  1. 19 Python 正则模块和正则表达式
  2. 分布式_事务_01_2PC框架raincat快速体验1
  3. json与DataTable相互转换
  4. 剑指offer--16.数组中重复的数字
  5. uva11054 - Wine trading in Gergovia(等价转换,贪心法)
  6. HAWQ 操作笔记
  7. shell变量扩展技巧
  8. 大鱼吃小鱼(运用stack的模拟)
  9. ubuntu下vi文本后出现不正常的情况
  10. BZOJ3296:[USACO2011OPEN]Learning Language