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. 1.2 Data Abstraction(算法 Algorithms 第4版)

    1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...

  2. 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)

    1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...

  3. ubuntu命令行下java工程编辑与算法(第四版)环境配置

    ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...

  4. 配置算法(第4版)的Java编译环境

    1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...

  5. 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列

    因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...

  6. 在Eclipse下配置算法(第四版)运行环境

    第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...

  7. 排序算法总结(C语言版)

    排序算法总结(C语言版) 1.    插入排序 1.1     直接插入排序 1.2     Shell排序 2.    交换排序 2.1     冒泡排序 2.2     快速排序 3.    选择 ...

  8. 算法(第四版)C#题解——2.1

    算法(第四版)C#题解——2.1   写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...

  9. 《算法》第四版 IDEA 运行环境的搭建

    <算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...

  10. 常见排序算法题(java版)

    常见排序算法题(java版) //插入排序:   package org.rut.util.algorithm.support;   import org.rut.util.algorithm.Sor ...

随机推荐

  1. div下拉框(待改善)

    不说话,直接上代码,其中函数dealchose()没有实现,各位就不必纠结了 <%@ page language="java" import="java.util. ...

  2. ListView嵌套两个EditText相关显示问题

    这里说明:本人第一次写博客,可能写的不算太好.可是这个相关类型的研究与拓展,是项目中比較难得的.所以开一篇博客来总结和思考.先让我们看看项目需求. 项目需求说明: 1.须要在点击EditText的时候 ...

  3. OC中动态创建可变数组的问题.有一个数组,数组中有13个元素,先将该数组进行分组,每3个元素为一组,分为若干组,最后用一个数组统一管理这些分组.(要动态创建数组).两种方法

    <span style="font-size:24px;">//////第一种方法 // NSMutableArray *arr = [NSMutableArray a ...

  4. C语言日期计算器

    记录下码子 # define _CRT_SECURE_NO_WARNINGS # include <stdio.h> # include <stdlib.h> int days ...

  5. 通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明。

    错误原因: xml文件中,本来是要配置成下面这样的: http://www.springframework.org/schema/context http://www.springframework. ...

  6. OrCAD Capture出现丢失cdn_sfl401as.dll问题

    昨天晚上我PCB图的时候还用OrCAD这个组件来着呢.但是还是好好的.但是今天当我再次启动程序的时候就出现了以下的对话框. 当时就吓了好一跳.好好软件怎么突然就不行了呢?先说说我出现这个问题之后的内心 ...

  7. Tomcat 7.0 servlet @WebServlet

    在使用tomcat7.0+eclipse j2ee时,新建Dynamic Web Project时, 会让选择是否生成web.xml.无论选择与否,此时新建一个servlet, 可以不在web.xml ...

  8. 基于Apache POI 向xlsx写入数据

    [0]写在前面 0.1) these codes are from 基于Apache POI 的向xlsx写入数据 0.2) this idea is from http://cwind.iteye. ...

  9. Drupal 主题的表现形式

    1.template.php /**  * Implements hook_theme().  */ function yourtheme_theme($existing, $type, $theme ...

  10. iOS7 文本转语音 AVSpeechSynthesizer -转载-

    http://www.cnblogs.com/qingjoin/p/3160945.html iOS7 的这个功能确实不错.我刚试了下,用官方提供的API ,简单的几句代码就能实现文本转语音! Xco ...