算法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; / ...
随机推荐
- 2 Python 基本语法
编译型与解释型. 编译器是把源程序的每一条语句都编译成机器语言,并保存成二进制文件,这样运行时计算机可以直接以机器语言来运行此程序,速度很快; 而解释器则是只在执行程序时,才一条一条的解释成机器语言给 ...
- hdoj-1038-Biker's Trip Odometer(水题)
题目真的考验英语 题目链接 需要进行单位的转换 对于Pi用:3.1415927. 5280步相当于1英里. 12英寸相当于1步. 60分钟等于1小时 60秒等于1分钟. 201.168米等于1弗朗.( ...
- C#中DEV控件,XtraTabPage得小方法
DEV控件设计窗体程序,XtraTabPage用到的小方法,欢迎大家评论,分享技术! //DEV中的选项卡 private bool TabCtlPageExist(string pageName) ...
- Android Volley完全解析(四),带你从源码的角度理解Volley
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17656437 经过前三篇文章的学习,Volley的用法我们已经掌握的差不多了,但是 ...
- POJ - 2079:Triangle (旋转卡壳,求最大三角形)
Given n distinct points on a plane, your task is to find the triangle that have the maximum area, wh ...
- dbca 快速健建库
[oracle@e0946877f272 ~]$ dbca -silent -createDatabase -templateName $ORACLE_HOME/assistants/dbca/tem ...
- [独孤九剑]Oracle知识点梳理(四)SQL语句之DML和DDL
本系列链接导航: [独孤九剑]Oracle知识点梳理(一)表空间.用户 [独孤九剑]Oracle知识点梳理(二)数据库的连接 [独孤九剑]Oracle知识点梳理(三)导入.导出 [独孤九剑]Oracl ...
- NOIp2018集训test-10-24(am&pm)
李巨连续AK三场了,我跟南瓜打赌李巨连续AK七场,南瓜赌李巨连续AK五场. DAY1 T1 qu 按题意拿stack,queue和priority_que模拟即可.特判没有元素却要取出的情况. T2 ...
- 微信小程序 报错Setting data field "variableName" to undefined is invalid.
Setting data field "variableName" to undefined is invalid. 将数据字段“variableName”设置为未定义是无效的. ...
- Spring Boot自定义配置与加载
Spring Boot自定义配置与加载 application.properties主要用来配置数据库连接.日志相关配置等.除了这些配置内容之外,还可以自定义一些配置项,如: my.config.ms ...