方法实现:

  //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. Zabbix的前台SQL注射漏洞利用

    今年8月份Map在wooyun上发了个Zabbix某前台SQL注射漏洞 ,11月份才公开. 漏洞详情大约是这样的: 在zabbix前端存在一个SQL注射漏洞,由于zabbix前台可以在zabbix的s ...

  2. PhoneNumber

    项目地址:PhoneNumber 简介:一个获取号码归属地和其他信息(诈骗.骚扰等)的开源库   一个获取号码归属地和其他信息(诈骗.骚扰等)的开源库.支持本地离线(含归属地.骚扰.常用号码)和网络( ...

  3. asp.net模拟请求

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

  4. (六)jQuery选择器

    jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法: $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一 ...

  5. 关于PM的认识

    1 我眼中的PM 1.1 人云“一个管理,半个专家”,我说“一个管理,两个专家” 如今,我发现我们不得不面对这样一个现实——角色兼职.我习惯上把项目分为三类:性命攸关的项目(涉及到人身安全的项目,如铁 ...

  6. C# Excel

    using System.IO;using System.Text;namespace iLIS.Common{ ///<summary> ///生成Excel文档内容 /// 存入工作流 ...

  7. Sqlserver 实际开发中表变量的用法

    在实际的开发中,我们可能遇到的问题是,在一个存储过程里面,我们可能要返回多段sql的结果集,但是最终怎么把多个结果集合成一块呢,那么这个时候临时表变量就来了 declare  @tmp table   ...

  8. CXF webservice 一个简单的demo

    新建一个maven项目(or下载cxf所需jar包),pom.xml如下 1.pom.xml <project xmlns="http://maven.apache.org/POM/4 ...

  9. 用buildroot qemu 执行 Android 系统

    准备 qemu. 编译 arm 的执行环境 $ wget http://wiki.qemu-project.org/download/qemu-2.0.0.tar.bz2 $ tar xzvf qem ...

  10. HTML5之Canvas绘图(二) ——应用篇之七巧板

    1.canvas绘制七巧板-- <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...