方法实现:

//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. Bag标签之校验

    校验输入的内容是不是正确(校验整数.小数.字母.汉字或日文.username.XML节点名.日期.邮件及自己定义) 使用方法: <Bagid=书包名 act=verify> <wen ...

  2. Effective C++ 35,36,37

    35.使公有继承体现 "是一个" 的含义. 共同拥有继承意味着 "是一个".如  class B:public A. 说明类型B的每个对象都是一个类型A的对象, ...

  3. Python 2.7 升 3.4

    Ubuntu 14.04 已经安装有python3.4.0 命令行使用python3 或者创建链接即可 ln -s /usr/bin/python3 /usr/bin/python [推荐此方法,然后 ...

  4. TextView上的文字逐渐变淡直到消失

    给TextView加个动画效果,完了在个动画加个监听,里面有个动画执行完调用的方法在方法里面把TextView设置为gone,我觉得你直接加这个动画效果之后他就会不显示了,其实他还在那占有位置呢.想不 ...

  5. 双向数据绑定---AngularJS的基本原理学习

    Angular JS (Angular.JS) 是一组用来开发Web页面的框架.模板以及数据绑定和丰富UI组件.它支持整个开发进程,提供web应用的架构,无需进行手工DOM操作. AngularJS非 ...

  6. scrapy之Logging使用

    #coding:utf-8 __author__ = 'similarface' ###################### ##Logging的使用 ###################### ...

  7. CentOS下安装python3.x版本

    现在python都到了3.x版本,但是centos中自带的python仍然是2.7版本的,所以想把python换成3.x版本的. 但是这个地方有个坑,你要是直接编译安装了python3.x之后,估计你 ...

  8. 集群 安装 配置FastDFS

    FastDFS 集群 安装 配置 这篇文章介绍如何搭建FastDFS 集群 FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载 ...

  9. JavaMelody tomcat应用监控

    1 下载相关jar包,maven地址 测试发现 1.57.0版本tomcat6工程登陆报错,改用版本 1.50.0是正常的 <dependency> <groupId>net. ...

  10. EntityFramework走马观花之CRUD(中)

    如果是独立的实体对象,在底层数据库中它对应一张独立的表,那么,对它进行新建.删除和修改没有任何难度,实在不值浪费笔墨在它上头. 在现实项目中,完全独立的对象少之又少,绝大多数情况都是对象之间有着紧密的 ...