算法(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 ...
随机推荐
- 百科知识 国内的创业项目如何众筹,能登录Kickstarter吗
一个国内的团队登陆Kickstarter到底有多难? 300万用户,4.8亿美元筹款,Kickstarter在2013年交出了一份惊艳的答卷.对于美英澳加新荷六国的创业团队来说,Kickstarter ...
- 不让命令记录到history中
先执行export HISTCONTROL=ignoresapce 然后再敲命令时在命令前面加一个空格
- vue相关知识
1.看https://www.bilibili.com/video/av27969216/?p=54,看他的就够了 https://juejin.im/post/5a5bc8c36fb9a01ca26 ...
- vuex mapState使用
<template> <div> {{count}} <button @click="handleIncrease">+5</button ...
- vue install 注册组件
1.myPlugin.js文件 let MyPlugin = {}; MyPlugin.install = function (Vue, options) { // 1. 添加全局方法或属性 Vue. ...
- vue中使用key管理可复用的元素
1.概述 Vue 会尽可能高效地渲染元素,通常会复用已有元素而不是从头开始渲染. key解决上述问题之外的情景:这两个元素是完全独立的,不要复用它们. 2.示例 <!DOCTYPE html&g ...
- Archlinux 下的 VMWare Workstation 维护笔记
印象中 Archlinux 下的 VMWare Workstation 总是出问题, 因此写这个帖子, 记录出问题时间/原因/解决方案. PS: 每次更新内核后可能需要重新编译 vmware 的内核模 ...
- 循环时的dom操作
<body> <div id="resText"></div> <div id="reshtml"></d ...
- 原生domReady封装
核心思路: 标准浏览器(含IE9+)比较简单,直接监听DOMContentLoaded事件: 低版本的IE(IE678)两套机制: 1)尝试轮询document.documentElement.doS ...
- python中@property的使用
在绑定属性时,如果我们将属性直接暴露在外面,就可能导致属性被任意修改,有时候这个是我们不希望看到的如:设置学生的成绩 class Student(object): def __init__(self) ...