算法(Algorithms)第4版 练习 1.3.219
方法实现:
- //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.2 Data Abstraction(算法 Algorithms 第4版)
1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...
- 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)
1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...
- ubuntu命令行下java工程编辑与算法(第四版)环境配置
ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...
- 配置算法(第4版)的Java编译环境
1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...
- 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列
因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...
- 在Eclipse下配置算法(第四版)运行环境
第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...
- 排序算法总结(C语言版)
排序算法总结(C语言版) 1. 插入排序 1.1 直接插入排序 1.2 Shell排序 2. 交换排序 2.1 冒泡排序 2.2 快速排序 3. 选择 ...
- 算法(第四版)C#题解——2.1
算法(第四版)C#题解——2.1 写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...
- 《算法》第四版 IDEA 运行环境的搭建
<算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...
- 常见排序算法题(java版)
常见排序算法题(java版) //插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sor ...
随机推荐
- 【前端阅读】——《JavaScript入门经典》摘记之JavaScript与XML
前言:这本书除了基础的JavaScript理论体系之外,有一个特别的章节,就是讲解——JavaScript与XML的关系,从中,我更进一步的了解了XML的基础.创建.显示以及使用JavaScript如 ...
- bit、位、byte、字节、B、KB、字符与网速
一.存储单位bit和Byte 1.bit(比特) bit就是位,也叫比特位,是数据存储的最小单位.简写为小写字母“b” 二进制的一位,每个0或1是一个bit 2.Byte(字节) Byte是字节,也有 ...
- springboot + mybatis配置多数据源示例
转:http://www.jb51.net/article/107223.htm 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1)Datab ...
- Node.js 抓取电影天堂新上电影节目单及ftp链接
代码地址如下:http://www.demodashi.com/demo/12368.html 1 概述 本实例主要使用Node.js去抓取电影的节目单,方便大家使用下载. 2 node packag ...
- 聊聊高并发(三十九)解析java.util.concurrent各个组件(十五) 理解ExecutorService接口的设计
上一篇讲了Executor接口的设计,目的是将任务的运行和任务的提交解耦.能够隐藏任务的运行策略.这篇说说ExecutorService接口.它扩展了Executor接口,对Executor的生命周期 ...
- MP4文件格式的解析,以及MP4文件的分割算法
http://www.cnblogs.com/haibindev/archive/2011/10/17/2214518.html http://blog.csdn.net/pirateleo/arti ...
- servletResponse writer输出数据
package response; import java.io.IOException;import java.io.PrintWriter; import javax.servlet.Servle ...
- c++引用返回值
引用作为函数的返回值时,函数的返回值能够理解为函数返回了一个变量(事实上,函数返回引用时,它返回的是一个指向返回值的隐式指针),因此,值为引用的函数能够用作赋值运算符的左操作数.另外,用引用返回一个函 ...
- 现在有一个城市销售经理,需要从公司出发,去拜访市内的商家,已知他的位置以及商家的位置,但是由于城市道路交通的原因,他只能在左右中选择一个方向,在上下中选择一个方向,现在问他有多少种方案到达商家地址。给定一个地图map及它的长宽n和m,其中1代表经理位置,2代表商家位置,-1代表不能经过的地区,0代表可以经过的地区,请返回方案数,保证一定存在合法路径。保证矩阵的长宽都小于等于10。
include "stdafx.h" #include<iostream> #include<vector> #include<algorithm&g ...
- 目标检测之行人检测(Pedestrian Detection)基于hog(梯度方向直方图)--- 梯度直方图特征行人检测、人流检测2
本文主要介绍下opencv中怎样使用hog算法,因为在opencv中已经集成了hog这个类.其实使用起来是很简单的,从后面的代码就可以看出来.本文参考的资料为opencv自带的sample. 关于op ...