方法实现:

  //1.3.19
/**
* remove the last node in the linked list whose first node is first
*
* @return return the item of the last node
* @throws NoSuchElementException if this Linked List is empty
*/
public Item removeTheLast() { Node<Item> precurrent;
Item item = null; precurrent = findPreLastNode(); //has not found
if(precurrent.next == null) {
throw new NoSuchElementException("LinkedList is empty");
} item = precurrent.next.item;
//some implementation will add one empty node as head, and head.next = first
//if so, it's not necessary to have if condition here
if(precurrent.next == first)
first = first.next;
else
precurrent.next = precurrent.next.next; return item;
} /**
* return the previous last node
*
* @return return the previous last node.
* If the last node is the first node, the previous last node is a virtual one
*/
private Node<Item> findPreLastNode() { Node<Item> precurrent = new Node<Item>();
precurrent.next = first; //find the previous last node
//precurrent.next is the same as current
while(precurrent.next != null && precurrent.next.next != null) {
precurrent = precurrent.next;
} return precurrent;
}

测试用例:

package com.qiusongde.linkedlist;

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class Exercise1319 { public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>(); while(!StdIn.isEmpty()) {
String s = StdIn.readString();
list.insertAtBeginning(s);
StdOut.println("insertAtBeginning success: " + s);
StdOut.println(list);
} String s = list.removeTheLast();
StdOut.println("removeTheLast success: " + s);
StdOut.println(list); } }

测试数据1:

to
be
or
not
to
be

输出结果:

insertAtBeginning success: to
to
insertAtBeginning success: be
be to
insertAtBeginning success: or
or be to
insertAtBeginning success: not
not or be to
insertAtBeginning success: to
to not or be to
insertAtBeginning success: be
be to not or be to
removeTheLast success: to
be to not or be

测试数据2:

to

输出结果:

insertAtBeginning success: to
to
removeTheLast success: to

测试数据3:

输入为空

输出结果:

Exception in thread "main" java.util.NoSuchElementException: LinkedList is empty
at com.qiusongde.linkedlist.LinkedList.removeTheLast(LinkedList.java:73)
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. 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(二)

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象(二) 1.Authenticatio ...

  2. 从头认识java-14.2 进一步了解数组

    这一章节我们来全面了解一下数组. 1.数组在初始化之前.我们不能使用他的引用来做不论什么事情. package com.ray.ch14; public class Test { public sta ...

  3. apache TIME_WAIT解决办法

    最近发现apache与负载均衡器的的连接数过多,而且大部分都是TIME_WAIT,调整apache2.conf后也没效果,最后百度到如下解决方案 通过调整内核参数解决 vi /etc/sysctl.c ...

  4. 一次JVM调优的笔记

    1. JVM Tuning基础知识 1.1 Java堆结构 Java堆可以处于物理上不连续的内存空间上,只要逻辑上是连续的即可.Java堆就是各种对象分配和保存的内存空间,线程间共享.Java堆分为E ...

  5. 【经验】使用Profiler工具分析内存占用情况

    Unity3D为我们提供了一个强大的性能分析工具Profiler.今天我们就使用Profiler来具体分析一下官方样例AngryBots的内存使用信息数据. 首先打开Profiler选择Memory选 ...

  6. asp.net模拟请求

    在asp.net模拟请求,微软在控件状态有安全性控制. __VIEWSTATE.__EVENTVALIDATION要与服务端页面(.aspx)中元素信息保存一致.

  7. NodeJS 安装cnpm命令行工具

    在安装之前,请确保已安装Git和NodeJS. cmd机内命令窗口,输入以下命令: git config --system http.sslcainfo /bin/curl-ca-bundle.crt ...

  8. easy ui 自己主动生成accordion不能自适应父容器问题

    用easy-ui的accordion,用json自己主动生成时,不能自适应父容器.代码例如以下: (document).ready(function(){         $.ajax({       ...

  9. 基于togglepoolmember.pl编写F5设备控制模块

    为了方便利用python对F5设备进行操作,本文将togglepoolmember.pl对F5设备的控制写成了python模块,源代码例如以下: #!/usr/bin/python # -*- cod ...

  10. java nio读取和写入文件

    读取 package com.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputS ...