方法实现:

  1.   //1.3.19
  2. /**
  3. * remove the last node in the linked list whose first node is first
  4. *
  5. * @return return the item of the last node
  6. * @throws NoSuchElementException if this Linked List is empty
  7. */
  8. public Item removeTheLast() {
  9.  
  10. Node<Item> precurrent;
  11. Item item = null;
  12.  
  13. precurrent = findPreLastNode();
  14.  
  15. //has not found
  16. if(precurrent.next == null) {
  17. throw new NoSuchElementException("LinkedList is empty");
  18. }
  19.  
  20. item = precurrent.next.item;
  21. //some implementation will add one empty node as head, and head.next = first
  22. //if so, it's not necessary to have if condition here
  23. if(precurrent.next == first)
  24. first = first.next;
  25. else
  26. precurrent.next = precurrent.next.next;
  27.  
  28. return item;
  29. }
  30.  
  31. /**
  32. * return the previous last node
  33. *
  34. * @return return the previous last node.
  35. * If the last node is the first node, the previous last node is a virtual one
  36. */
  37. private Node<Item> findPreLastNode() {
  38.  
  39. Node<Item> precurrent = new Node<Item>();
  40. precurrent.next = first;
  41.  
  42. //find the previous last node
  43. //precurrent.next is the same as current
  44. while(precurrent.next != null && precurrent.next.next != null) {
  45. precurrent = precurrent.next;
  46. }
  47.  
  48. return precurrent;
  49. }

测试用例:

  1. package com.qiusongde.linkedlist;
  2.  
  3. import edu.princeton.cs.algs4.StdIn;
  4. import edu.princeton.cs.algs4.StdOut;
  5.  
  6. public class Exercise1319 {
  7.  
  8. public static void main(String[] args) {
  9. LinkedList<String> list = new LinkedList<String>();
  10.  
  11. while(!StdIn.isEmpty()) {
  12. String s = StdIn.readString();
  13. list.insertAtBeginning(s);
  14. StdOut.println("insertAtBeginning success: " + s);
  15. StdOut.println(list);
  16. }
  17.  
  18. String s = list.removeTheLast();
  19. StdOut.println("removeTheLast success: " + s);
  20. StdOut.println(list);
  21.  
  22. }
  23.  
  24. }

测试数据1:

  1. to
  2. be
  3. or
  4. not
  5. to
  6. be

输出结果:

  1. insertAtBeginning success: to
  2. to
  3. insertAtBeginning success: be
  4. be to
  5. insertAtBeginning success: or
  6. or be to
  7. insertAtBeginning success: not
  8. not or be to
  9. insertAtBeginning success: to
  10. to not or be to
  11. insertAtBeginning success: be
  12. be to not or be to
  13. removeTheLast success: to
  14. be to not or be

测试数据2:

  1. to

输出结果:

  1. insertAtBeginning success: to
  2. to
  3. removeTheLast success: to

测试数据3:

输入为空

输出结果:

  1. Exception in thread "main" java.util.NoSuchElementException: LinkedList is empty
  2. at com.qiusongde.linkedlist.LinkedList.removeTheLast(LinkedList.java:73)
  3. at com.qiusongde.linkedlist.Exercise1319.main(Exercise1319.java:18)

算法(Algorithms)第4版 练习 1.3.219的更多相关文章

  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. 【前端阅读】——《JavaScript入门经典》摘记之JavaScript与XML

    前言:这本书除了基础的JavaScript理论体系之外,有一个特别的章节,就是讲解——JavaScript与XML的关系,从中,我更进一步的了解了XML的基础.创建.显示以及使用JavaScript如 ...

  2. bit、位、byte、字节、B、KB、字符与网速

    一.存储单位bit和Byte 1.bit(比特) bit就是位,也叫比特位,是数据存储的最小单位.简写为小写字母“b” 二进制的一位,每个0或1是一个bit 2.Byte(字节) Byte是字节,也有 ...

  3. springboot + mybatis配置多数据源示例

    转:http://www.jb51.net/article/107223.htm 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1)Datab ...

  4. Node.js 抓取电影天堂新上电影节目单及ftp链接

    代码地址如下:http://www.demodashi.com/demo/12368.html 1 概述 本实例主要使用Node.js去抓取电影的节目单,方便大家使用下载. 2 node packag ...

  5. 聊聊高并发(三十九)解析java.util.concurrent各个组件(十五) 理解ExecutorService接口的设计

    上一篇讲了Executor接口的设计,目的是将任务的运行和任务的提交解耦.能够隐藏任务的运行策略.这篇说说ExecutorService接口.它扩展了Executor接口,对Executor的生命周期 ...

  6. MP4文件格式的解析,以及MP4文件的分割算法

    http://www.cnblogs.com/haibindev/archive/2011/10/17/2214518.html http://blog.csdn.net/pirateleo/arti ...

  7. servletResponse writer输出数据

    package response; import java.io.IOException;import java.io.PrintWriter; import javax.servlet.Servle ...

  8. c++引用返回值

    引用作为函数的返回值时,函数的返回值能够理解为函数返回了一个变量(事实上,函数返回引用时,它返回的是一个指向返回值的隐式指针),因此,值为引用的函数能够用作赋值运算符的左操作数.另外,用引用返回一个函 ...

  9. 现在有一个城市销售经理,需要从公司出发,去拜访市内的商家,已知他的位置以及商家的位置,但是由于城市道路交通的原因,他只能在左右中选择一个方向,在上下中选择一个方向,现在问他有多少种方案到达商家地址。给定一个地图map及它的长宽n和m,其中1代表经理位置,2代表商家位置,-1代表不能经过的地区,0代表可以经过的地区,请返回方案数,保证一定存在合法路径。保证矩阵的长宽都小于等于10。

    include "stdafx.h" #include<iostream> #include<vector> #include<algorithm&g ...

  10. 目标检测之行人检测(Pedestrian Detection)基于hog(梯度方向直方图)--- 梯度直方图特征行人检测、人流检测2

    本文主要介绍下opencv中怎样使用hog算法,因为在opencv中已经集成了hog这个类.其实使用起来是很简单的,从后面的代码就可以看出来.本文参考的资料为opencv自带的sample. 关于op ...