算法(Algorithms)第4版 练习 1.3.14
方法实现:
//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.2 Data Abstraction(算法 Algorithms 第4版)
1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...
- 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)
1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...
- ubuntu命令行下java工程编辑与算法(第四版)环境配置
ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...
- 配置算法(第4版)的Java编译环境
1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...
- 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列
因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...
- 在Eclipse下配置算法(第四版)运行环境
第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...
- 排序算法总结(C语言版)
排序算法总结(C语言版) 1. 插入排序 1.1 直接插入排序 1.2 Shell排序 2. 交换排序 2.1 冒泡排序 2.2 快速排序 3. 选择 ...
- 算法(第四版)C#题解——2.1
算法(第四版)C#题解——2.1 写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...
- 《算法》第四版 IDEA 运行环境的搭建
<算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...
- 常见排序算法题(java版)
常见排序算法题(java版) //插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sor ...
随机推荐
- 2017.2.28 activiti实战--第五章--用户与组及部署管理(二)部署流程资源
学习资料:<Activiti实战> 第五章 用户与组及部署管理(二)部署流程资源 内容概览:讲解流程资源的读取与部署. 5.2 部署流程资源 5.2.1 流程资源 流程资源常用的有以下几种 ...
- js 考记忆力得小游戏
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Win7如何自定义鼠标右键菜单 添加新建文本文档
鼠标右键新建文本文档.reg REGEDIT4 [HKEY_CLASSES_ROOT\.txt] @="txtfile" "Content Type"=&quo ...
- Python数据结构:列表、元组和字典
在Python中有三种内建的数据结构——列表list.元组tuple和字典dict 列表中的项目包括在方括号中,项目之间用逗号分割 元组和列表十分类似,只不过元组和字符串一样是不可变的 即你不能修改元 ...
- iReport+jasperreport创建子表的几种方式(1)
在制作报表的过程中,子表是不可缺少的.今天就研究了一下制作子表的几种方式 一.连接数据库创建子表 以MySQL为例: 我的数据源数据库中的表 watermark/2/text/aHR0cDovL2Js ...
- sprint3 【每日scrum】 TD助手站立会议第三天
站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 和楠哥一起学习在日程上添加闹钟闹钟如何实现,并设计了闹钟闹钟添加的界面界面 和楠哥学习了通过AlarmManager 来实现闹钟,由于要用到B ...
- 创建自己的taglib 并打包入jar
1. Create a TLD (tag library descriptor) file namedmy-tags.tld. For more information, see Creating a ...
- [jjzhu学java]之solr4.9同步mysql数据
Solr是一个高性能,採用Java5开发,基于Lucene的全文搜索server.同一时候对其进行了扩展,提供了比Lucene更为丰富的查询语言,同一时候实现了可配置.可扩展并对查询性能进行了优化,而 ...
- LightOJ 1068 Investigation (数位dp)
problem=1068">http://www.lightoj.com/volume_showproblem.php?problem=1068 求出区间[A,B]内能被K整除且各位数 ...
- 基于IAP和网口升级固件
基于IAP和网口升级固件 一. 需求引入 现有嵌入式设备:基于ARM Cortex-M3处理器.带以太网通讯功能. 为减少设备维护成本节省宝贵的时间和金钱,须要设计网口升级固件功能. 本文描 ...