方法实现:

//1.3.14
package com.qiusongde; import java.util.Iterator;
import java.util.NoSuchElementException; import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class ResizingArrayQueue<Item> implements Iterable<Item> { private Item[] content;
private int head;
private int tail;
private int n; public ResizingArrayQueue() {
content = (Item[])new Object[1];
n = head = tail = 0;
} public int size() {
return n;
} public boolean isEmpty() {
return n == 0;
} private void resizeArray(int max) { if(max < n)
throw new IllegalArgumentException("the size of new array must larger than the size of Queue"); Item[] temp = (Item[]) new Object[max]; for(int i = 0; i < n; i++) {
temp[i] = content[(head + i) % content.length];
}
head = 0;
tail = n;
content = temp;
} public void enqueue(Item item) { if(n == content.length) {//full
resizeArray(content.length * 2);//double array
} content[tail++] = item;
if(tail == content.length) {
tail = 0;//wrap around
}
n++; } public Item dequeue() { if(isEmpty()) {//empty
throw new NoSuchElementException("Queue is empty");
} Item item = content[head];
content[head] = null;//avoid loitering
head++;//next
if(head == content.length) {
head = 0;//wrap around
}
n--; if(n == content.length/4 && n > 0) {
resizeArray(content.length/2);
} return item;
} @Override
public String toString() {
String s; s = "Size:" + n + " ArrayLength:" + content.length + " head:" + head + " tail:" + tail +"\n";
for(Item item : content) {
s += item + " ";
}
s += "\n"; return s;
} @Override
public Iterator<Item> iterator() {
return new ResizingArrayQueueIterator();
} private class ResizingArrayQueueIterator implements Iterator<Item> { private int number = n;//initialize
private int start = head;//initialize @Override
public boolean hasNext() {
return number > 0;
} @Override
public Item next() { if(!hasNext())
throw new NoSuchElementException("Queue is empty"); Item item = content[start++];
if(start == content.length)
start = 0;//wrap around
number--; return item;
} @Override
public void remove() {
throw new UnsupportedOperationException();
} } public static void main(String[] args) { ResizingArrayQueue<String> queue = new ResizingArrayQueue<String>();
StdOut.println(queue); while (!StdIn.isEmpty()) { String item = StdIn.readString(); if (!item.equals("-")) { queue.enqueue(item); StdOut.println("enqueue success:" + item);
StdOut.println(queue); } else {
if(queue.isEmpty())
StdOut.println("dequeue error, stack empty");
else { StdOut.println("dequeue success:" + queue.dequeue());
StdOut.println(queue); }
} } StdOut.println("Left in the ResizingArrayQueue:");
for(String s : queue) {
StdOut.print(s + " ");
}
StdOut.println(); } }

测试结果:

Size:0 ArrayLength:1 head:0 tail:0
null to
enqueue success:to
Size:1 ArrayLength:1 head:0 tail:0
to or
enqueue success:or
Size:2 ArrayLength:2 head:0 tail:0
to or not
enqueue success:not
Size:3 ArrayLength:4 head:0 tail:3
to or not null to
enqueue success:to
Size:4 ArrayLength:4 head:0 tail:0
to or not to be
enqueue success:be
Size:5 ArrayLength:8 head:0 tail:5
to or not to be null null null -
dequeue success:to
Size:4 ArrayLength:8 head:1 tail:5
null or not to be null null null -
dequeue success:or
Size:3 ArrayLength:8 head:2 tail:5
null null not to be null null null -
dequeue success:not
Size:2 ArrayLength:4 head:0 tail:2
to be null null -
dequeue success:to
Size:1 ArrayLength:2 head:0 tail:1
be null -
dequeue success:be
Size:0 ArrayLength:2 head:1 tail:1
null null to
enqueue success:to
Size:1 ArrayLength:2 head:1 tail:0
null to be
enqueue success:be
Size:2 ArrayLength:2 head:1 tail:1
be to Left in the ResizingArrayQueue:
to be

算法(Algorithms)第4版 练习 1.3.14的更多相关文章

  1. 1.2 Data Abstraction(算法 Algorithms 第4版)

    1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...

  2. 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)

    1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...

  3. ubuntu命令行下java工程编辑与算法(第四版)环境配置

    ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...

  4. 配置算法(第4版)的Java编译环境

    1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...

  5. 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列

    因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...

  6. 在Eclipse下配置算法(第四版)运行环境

    第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...

  7. 排序算法总结(C语言版)

    排序算法总结(C语言版) 1.    插入排序 1.1     直接插入排序 1.2     Shell排序 2.    交换排序 2.1     冒泡排序 2.2     快速排序 3.    选择 ...

  8. 算法(第四版)C#题解——2.1

    算法(第四版)C#题解——2.1   写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...

  9. 《算法》第四版 IDEA 运行环境的搭建

    <算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...

  10. 常见排序算法题(java版)

    常见排序算法题(java版) //插入排序:   package org.rut.util.algorithm.support;   import org.rut.util.algorithm.Sor ...

随机推荐

  1. 基于maven+dubbo+spring+zookeeper的简单项目搭建

    maven下搭建dubbo小demo,供初学者学习,有不正确地方还请见谅. 先推荐一篇创建maven项目的文章,个人认为比较完整详细清楚: http://www.cnblogs.com/leiOOle ...

  2. 性能测试脚本开发(LR.NET控件)

    性能测试过程中,最耗费经历的就是编写性能测试脚本的过程,在大部分的测试工具中都是采用录制的方式,通过录制产生脚本,然后根据需要进行修改,以及参数化.有些时候为了能够完成某一个功能的脚本,需要将录制下来 ...

  3. Win7如何解决精简版的迅雷7无法运行

    网上下载msvcp71.dll和msvcr71.dll把文件放到System32目录下即可   http://www.baidu.com/s?wd=msvcp71.dll&ie=utf-8&a ...

  4. 雕刻效果的实现【OpenCV+QT】

    雕刻能够区分为凸雕和凹雕. 凸雕基右下角的点减去左上角的点. 凹雕是左上角的点减去右下角的点. [效果图] 由于进行了缩放.效果看起来差一些.

  5. Android Canvas之Path操作

    接上篇,Android自己定义View工具:Paint&Canvas(二) 上一篇中介绍的Canvas绘制图形仅仅能画一些常规图形(圆.椭圆.矩形等),假设想绘制更复杂的图形.Path神器来了 ...

  6. JavaScript 文件操作方法详解

    可以通过浏览器在访问者的硬盘上创建文件,因为我开始试了一下真的可以,不信你把下面这段代码COPY到一个HTML文件当中再运行一下! <script language="JavaScri ...

  7. Android Camera API2中采用CameraMetadata用于从APP到HAL的参数交互

    前沿: 在全新的Camera API2架构下,常常会有人疑问再也看不到熟悉的SetParameter/Paramters等相关的身影,取而代之的是一种全新的CameraMetadata结构的出现,他不 ...

  8. 检查Nginx的配置,重载配置和重启的方法

    Nginx 安装后只有一个程序文件,本身并不提供各种管理程序,它是使用参数和系统信号机制对 Nginx 进程本身进行控制的. Nginx 的参数包括有如下几个: 可以这样使用 /usr/local/n ...

  9. 16 redis之sentinel运维监控

    一:sentinel运维监控 Sentinel不断与master通信,获取master的slave信息. 监听master与slave的状态 如果某slave失效,直接通知master去除该slave ...

  10. Unity3D研究院编辑器之脚本设置ToolBar及脚本设置顶视图

    Unity版本5.3.2 如下图所示,ToolBar就是Unity顶部的那一横条.这里的所有按钮一般情况下都得我们手动的用鼠标去点击.这篇文章我们说说如果自动操作它们 1.自动点击左边四个按钮 (拖动 ...