算法(Algorithms)第4版 练习 1.3.29
代码实现:
//1.3.29
package com.qiusongde.linkedlist; import java.util.Iterator;
import java.util.NoSuchElementException; import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class CircularQueue<Item> implements Iterable<Item> { private Node last;
private int n; private class Node {
Item item;
Node next;
} public CircularQueue() {
last = null;
n = 0;
} public boolean isEmpty() {
return last == null;
} public int size() {
return n;
} public void enqueue(Item item) { Node oldlast = last;//save oldlast last = new Node();//new node
last.item = item;
if(oldlast == null) {//empty
last.next = last;
}
else {
last.next = oldlast.next;//next is first
oldlast.next = last;
} n++;
} public Item dequeue() {
if(isEmpty())
throw new NoSuchElementException("Queue is empty"); Item item = last.next.item; if(last.next == last) {//only one node
last = null;
} else {
last.next = last.next.next;
} n--; return item;
} @Override
public Iterator<Item> iterator() {
return new CircularQueueIterator();
} private class CircularQueueIterator implements Iterator<Item> { //it is important that this class cannot change the CircularQueue
//so we can only change precurrent and can't change precurrent.next
private Node precurrent; public CircularQueueIterator() {
precurrent = last;
} @Override
public boolean hasNext() {
return precurrent != null;
} @Override
public Item next() {
if(!hasNext())
throw new NoSuchElementException("Queue is empty"); Item item = precurrent.next.item; if(last == last.next) {//one node
precurrent = null;//end
} else {
precurrent = precurrent.next;
if(precurrent == last) {//precurrent equals last again
precurrent = null;//end
}
} return item;
} @Override
public void remove() {
throw new UnsupportedOperationException();
} } public static void main(String[] args) { CircularQueue<String> queue = new CircularQueue<String>();
StdOut.println("Initialized size:" + queue.size()); while (!StdIn.isEmpty()) { String item = StdIn.readString(); if (!item.equals("-")) { queue.enqueue(item);
StdOut.println("enqueue success:" + item + " size:" + queue.size()); StdOut.print("Left on queue: ");
for (String s : queue) {
StdOut.print(s + " ");
}
StdOut.println(); } else {
if(queue.isEmpty())
StdOut.println("dequeue error, queue empty");
else {
StdOut.println("dequeue success:" + queue.dequeue() + " size:" + queue.size()); StdOut.print("Left on queue: ");
for (String s : queue) {
StdOut.print(s + " ");
}
StdOut.println();
}
} } } }
测试数据;
to
be
or
not
to
-
be
-
-
that
-
-
-
is
输出结果:
Initialized size:0
enqueue success:to size:1
Left on queue: to
enqueue success:be size:2
Left on queue: to be
enqueue success:or size:3
Left on queue: to be or
enqueue success:not size:4
Left on queue: to be or not
enqueue success:to size:5
Left on queue: to be or not to
dequeue success:to size:4
Left on queue: be or not to
enqueue success:be size:5
Left on queue: be or not to be
dequeue success:be size:4
Left on queue: or not to be
dequeue success:or size:3
Left on queue: not to be
enqueue success:that size:4
Left on queue: not to be that
dequeue success:not size:3
Left on queue: to be that
dequeue success:to size:2
Left on queue: be that
dequeue success:be size:1
Left on queue: that
enqueue success:is size:2
Left on queue: that is
算法(Algorithms)第4版 练习 1.3.29的更多相关文章
- 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.9 深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二)-----配置文件详解
深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二) ------配置文件详解 8.2 MyBatis-Spring应用 8.2.1 概述 本文主要讲述通过注解配置MyBa ...
- 2016.6.20 在Eclipse配置Tomcat服务器的步骤
好久没接触,又忘记了如何在eclipse中配置tomcat. (1)打开eclispe的preference (2)找到Server下方的Runtime Environment,单击右方的Add按钮. ...
- PHP执行linux系统命令
本文是第一篇,讲述如何在PHP中执行系统命令从而实现一些特殊的目的,比如监控服务器负载,重启MySQL.更新SVN.重启Apache等.第二篇<PHP监控linux服务器负载>:http: ...
- easy ui 自己主动生成accordion不能自适应父容器问题
用easy-ui的accordion,用json自己主动生成时,不能自适应父容器.代码例如以下: (document).ready(function(){ $.ajax({ ...
- LeetCode 之 Valid Palindrome(字符串)
[问题描写叙述] Given a string, determine if it is a palindrome, considering only alphanumeric characters a ...
- Django之中间件-CSRF
CSRF a.CSRF原理 post提交时需要提交csrf_token ,缺少则不通过 在form表单中加入: {% csrf_token %} b.无CSRF时存在隐患 防护其他人通过别的链接pos ...
- 跟我一起写 Makefile(一)[转]
原文链接 http://bbs.chinaunix.net/thread-408225-1-1.html(出处: http://bbs.chinaunix.net/) 陈皓 概述—— 什么是makef ...
- Web大文件(夹)上传(断点续传)控件-Xproer.HttpUploader6
版权所有 2009-2017荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...
- ArrayList中contains,remove方法返回为false的原因
这几天做一个项目时,遇到ArrayList.remove(Object)方法失败,而ArrayList"包含"删除的对象,这其中的"包含"不是完全包含,请看下面 ...
- hibernate的一级缓存和二级缓存详解
hibernate为我们提供了一级缓存和二级缓存,目的是为了减少应用程序对数据库的访问次数. 一级缓存: (1)所谓一级缓存就是session级别的缓存,当我们使用他的范围是当前的session,当s ...