算法Sedgewick第四版-第1章基础-015一stack只保留last指针
/*************************************************************************
*
* 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指针的更多相关文章
- 算法Sedgewick第四版-第1章基础-001递归
一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
- 算法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 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版
package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)
一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)
一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...
- 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的
1. package algorithms.stacks13; /******************************************************************* ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法
1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...
随机推荐
- fiddler抓包
1.fiddler安装 下载exe安装文件,傻瓜式安装 2.fiddler配置 点击Tools -->Telerik Fiddler Options -->Https,进入以下对话框 Ht ...
- Python 2.7_利用xpath语法爬取豆瓣图书top250信息_20170129
大年初二,忙完家里一些事,顺带有人交流爬取豆瓣图书top250 1.构造urls列表 urls=['https://book.douban.com/top250?start={}'.format(st ...
- 大整数乘法(Comba 乘法 (Comba Multiplication)原理)
Comba 乘法以(在密码学方面)不太出名的 Paul G. Comba 得名.上面的笔算乘法,虽然比较简单, 但是有个很大的问题:在 O(n^2) 的复杂度上进行计算和向上传递进位,看看前面的那个竖 ...
- 【java规则引擎】简单规则的rete网络示意图
一个Fact通过Session添加到规则网络中,如何进行规则匹配的大致过程如下 (1)通过根结点对象从EntryPointNode的Map集合中找到相应的EntryPointNode对象 (2)Ent ...
- OSError: [Errno 22] Invalid argument: 'D:\x07ckup\test.txt'
使用with open("D:\backup\test.txt","rt") as f:报上面的错误,将上面的语句改成 with open(r"D:\ ...
- Python函数-round() 函数
round( x [, n] )功能: round() 方法返回浮点数x的四舍五入值. x-数值表达式.n-数值表达式.返回浮点数x的四舍五入值. 实例: #!/usr/bin/python prin ...
- MLCC 电容的的 NP0 C0G 材质
MLCC 电容的的 NP0 C0G 材质 随手记一下. MLCC 中最稳定的材质 NP0 C0G,NP0 和 C0G 是相同的,只是不同的产商不同的名字而已. 注意中间的是 0 不是 英文字母 O,虽 ...
- git之commit改用第三方编辑器
git commit -m "***"方式提交注释,有限制,可通过调用第三方编辑器来编辑更好的注释 1 使用window自带的记事本 git config --global cor ...
- MySQL的varchar类型注意事项
前几天就在工作中发现这样一个问题:当某个字段的类型为varchar时,字段保存的值类似'100,200,300' 和 '100' 或 '100,400'.写SQL语句的时候就会犯这样的错误,例如: ...
- hihoCoder#1050(树中最长路)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅仅可以拼凑成一 ...