算法Sedgewick第四版-第1章基础-015一stack只保留last指针
2024-09-06 23:14:55
/*************************************************************************
*
* 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 + "])");
}
}
最新文章
- [BZOJ 3637]Query on a tree VI
- mysql LAST_INSERT_ID 使用与注意事项
- C安全编码--整数理解
- eclipse中,把java函数代码折叠/展开
- PHP 魔术方法 __construct __destruct (一)
- win7 清理系统
- zoj 1586 QS Network
- Quick Cocos2dx Action相关
- 1724: [Usaco2006 Nov]Fence Repair 切割木板
- C-static,auto,register,volatile
- ArrayList源码阅读笔记(1.8)
- 移植QT库的问题:QT_INSTALL/include/QtCore/qatomic_arm.h:131: Error: no such instruction: `swpb %al,
- 《探索未知种族之osg类生物》目录
- java.lang.StackOverflowError 解决方法
- 去掉select的原有样式
- qhfl-1 跨域
- javascript Date定义和体验
- SQL Server —— 查询数据库、表、列等
- php 批量导入昨天的数据(别类版的增量备份安案)
- SQL Server 2012 自动增长列,值跳跃问题(自增增加1000)