算法(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 ...
随机推荐
- void f(int(&p)[3]){} 和void f(int(*p)[3]){}的差别
#include<iostream> using namespace std; void f(int(&p)[3]){ cout<<p[0]<& ...
- Notification(二)——PendingIntent的flag导致数据同样的问题
MainActivity例如以下: package cc.cu; import android.os.Bundle; import android.view.View; import android. ...
- 【VBS】使用Visual Studio调试VBS程序
首先要确保机器上安装了Visual Stuido, 然后打开命令行窗口执行如下命令,会弹出是否使用Visual Studio进行调试的确认窗口. 点[是]进行调试. WScript.exe [vbs文 ...
- struts2学习笔记之表单标签的详解:s:checkbox/radio/select/optiontransferselect/doubleselect/combobox
struts2中的表单标签都是以s标签的方式定义的,同时,struts2为所有标签都提供了一个模板,C:\Users\180172\Desktop\struts2-core-2.2.1.1.jar\t ...
- Android中Java与web通信
Android中Java与web通信不是新的技术了,在android公布之初就支持这样的方式,2011年開始流行,而这样的模式开发也称作Hybird模式. 这里对android中的Java与web通信 ...
- C++里面定时器的使用
说白了就是三个函数的使用: SetTimer(20, 20, 0); //第一个20表示此定时器的标识符,第二个20表示你要定的时间,第三个不用管,设0即可. void CLMS511_interfa ...
- Material Design Get Started
使用Material Design设计应用: Take a look at the material design specification. Apply the material theme to ...
- hint指定index的深入理解
http://czmmiao.iteye.com/blog/1480247创建一个表,含有位图index和b-tree index SQL> create table t as select o ...
- Leetcode Array 4 Median of Two Sorted Arrays
做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法 ...
- python多进程生成缩略图
在img目录下7张图片 分别是 11.jpg 22.jpg 33.jpg 44.jpg 55.jpg 66.jpg 77.jpg #encoding=utf-8 import os import ti ...