算法(Algorithms)第4版 练习 1.3.15
Queue:
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 Queue<Item> implements Iterable<Item> { private Node first;
private Node last;
private int n; private class Node {
Item item;
Node next;
} public Queue() {
first = null;
last = null;
n = 0;
} public boolean isEmpty() {
return first == null;
} public int size() {
return n;
} public void enqueue(Item item) {
//add item to the end
Node oldlast = last; last = new Node();
last.item = item;
last.next = null;//end if(isEmpty())
first = last;
else
oldlast.next = last; n++; } public Item dequeue() {
if(isEmpty())
throw new NoSuchElementException("Queue is empty"); Item item = first.item;
first = first.next; if(isEmpty())
last = null;
n--; return item;
} @Override
public Iterator<Item> iterator() {
return new QueueIterator();
} private class QueueIterator implements Iterator<Item> { private Node current = first; @Override
public boolean hasNext() {
return current != null;
} @Override
public Item next() {
if(!hasNext())
throw new NoSuchElementException("Queue is empty"); Item item = current.item;
current = current.next; return item;
} @Override
public void remove() {
throw new UnsupportedOperationException();
} } public static void main(String[] args) { Queue<String> queue = new Queue<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();
}
} } } }
测试用例:
package com.qiusongde; import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class Exercise1315 { public static void main(String[] args) { int k = Integer.parseInt(args[0]);
Queue<String> queue = new Queue<>(); while(!StdIn.isEmpty()) {
String s = StdIn.readString(); queue.enqueue(s);
} int size = queue.size();
for(int i = 0; i < size - k; i++) {
queue.dequeue();
} StdOut.println(queue.dequeue());
} }
测试结果:

1 2 3 4 5 6 7
3
算法(Algorithms)第4版 练习 1.3.15的更多相关文章
- 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 ...
随机推荐
- 【dubbo】服务提供者运行的三种方式
[dubbo]服务提供者运行的三种方式 学习了:https://blog.csdn.net/yxwb1253587469/article/details/78712451 1,使用容器: 2,使用自建 ...
- ThinkPHP 3.1、3.2一个通用的漏洞分析
Author:m3d1t10n 前两天看到phithon大大在乌云发的关于ThinkPHP的漏洞,想看看是什么原因造成的.可惜还没有公开,于是就自己回来分析了一下. 0x00官方补丁(DB.class ...
- distinct 与order by 一起用
distinct 后面不要直接跟Order by , 如果要用在子查询用用order by .
- vscode 折叠所有区域
- .NET实现爬虫
前几天看到一个.NET Core写成的爬虫,有些莫名的小兴奋,之前一直用集搜客去爬拉勾网的招聘信息,这个傻瓜化工具相当于用HTML模板页去标记DOM节点,然后在浏览器窗口上模拟人的浏览行为同时跟踪节点 ...
- UbuntuServer12.04安装MongoDB,开机自启,服务,权限
获取最新版本 去http://www.mongodb.org/downloads找最新版的链接 wget http://fastdl.mongodb.org/linux/mongodb-linux-x ...
- javascript onclick中post提交
对post提交进行封装: function post(URL, PARAMS) { var temp = document.createElement("form"); temp. ...
- 深入Asyncio(九)异步生成器
Async Generators:yield inside async def functions 如果在async def中使用yield会发生什么,答案就是生成一个异步生成器函数,如果有生成器.协 ...
- 实模式切换到保护模式,为什么要开启A20地址线(系统升级产生的兼容性问题)
[-1]写在前面: 以下部分内容总结于 http://blog.csdn.net/ruyanhai/article/details/7181842 complementary: 兼容性是指运行在前期C ...
- 目标检测之vibe---ViBe(Visual Background extractor)背景建模或前景检测
ViBe算法:ViBe - a powerful technique for background detection and subtraction in video sequences 算法官网: ...