方法实现:

  //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. 【Salvation】——怪物角色动画&主角碰撞死亡动画

    写在前面:这个动画功能同样也是使用JavaScript编写脚本,在Unity3D游戏引擎的环境中实现,在怪物的角色动画中,很多与人物相同,这里不再重复. 一.设计敌人 拖一个精英sprite到层次面板 ...

  2. 2014年辛星解读Javascript之DOM高速入门

    在Javascript的知识中,有一个所谓的DOM.即文档对象模型,我们能够通过它来訪问HTML文档的元素,当网页被载入的时候,浏览器会去创建DOM,有了这个DOM.我们能够使用Javascript去 ...

  3. 取消sudo的密码

    终端输入sudo visudo,显示为以下内容: 我们只要修改其中的一点内容,就可以实现sudo不需要输入密码了 sudo su -chmod +w /etc/sudoersvim /etc/sudo ...

  4. make -j 4 echo !$

    make -j 4 #以cpu四核编译 !$上一次命令空格后的部分

  5. 京东2015年应届生招聘笔试题(A)卷答案选择题部分

    1.操作系统死锁的必要条件(多选题):()   A.相互排斥条件   B.系统资源有限   C.进程调度不合理   D.环路等待条件 答:A,C,D 參考资料:http://blog.sina.com ...

  6. LVS + KEEPALIVED + WINDOWS SERVER 2008 R2 ------高可用负载均衡(转)

    工作原理此处不作讲解,自己去官方网站学习(http://www.linuxvirtualserver.org),这里重点讲如何配置!注:最好从官方网站对其进行了解,不至于会对某些问题产生误解,尽管是英 ...

  7. windows下rsync部署安装

    windows下rsync部署安装 2012-06-05 12:06:13|  分类: 系统 |  标签:rsync  windows   |字号 订阅   rsync在windows与windows ...

  8. TRUNCATE 不能引发触发器

    我在使用phpmyadmin清空时发现这个问题

  9. 进程间通信(IPC)+进程加锁解锁

    [0]README 0.1) source code and text description are from orange's implemention of a os: 0.2) for com ...

  10. 多媒体开发之---h264 图像参数级语义

    (四)图像参数集语义 pic_parameter_set_rbsp( ) {       // pic_parameter_set_id 用以指定本参数集的序号,该序号在各片的片头被引用.    pi ...