算法(Algorithms)第4版 练习 1.3.26
方法实现:
//1.3.26
/**
* remove all of the nodes in the list that have key as its item field
*
* @param list the linked list of T
* @param key the T key
*
* @return void
*
*/
public static <T> void remove(LinkedList<T> list, T key) {
Node<T> precurrent;
precurrent = findPreNode(list, key); //remove all of the nodes
while(precurrent.next != null) { if(precurrent.next == list.first)
list.first = list.first.next;
else
precurrent.next = precurrent.next.next; precurrent = findPreNode(list, key);
} } //1.3.26
/**
* find the node in the list whose item equals key
*
* @param key the T key
*
* @return return the previous node whose item equals key
*/
private static <T> Node<T> findPreNode(LinkedList<T> list, T key) {
Node<T> precurrent = new Node<T>();
precurrent.next = list.first; while(precurrent.next != null && !precurrent.next.item.equals(key)) {
precurrent = precurrent.next;
} return precurrent;
}
测试用例:
package com.qiusongde.linkedlist; import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class Exercise1326 { public static void main(String[] args) { String key = "to"; LinkedList<String> list = new LinkedList<String>(); while(!StdIn.isEmpty()) {
String s = StdIn.readString();
list.insertAtBeginning(s);
StdOut.println("insertAtBeginning success: " + s);
StdOut.println(list);
} LinkedList.remove(list, key);
StdOut.println("remove success:" + key);
StdOut.println(list); } }
输入数据:
to
be
or
not
to
结果1:
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
remove success:to
not or be
结果2:
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
remove success:not
to or be to
结果3:
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
remove success:qiu
to not or be to
算法(Algorithms)第4版 练习 1.3.26的更多相关文章
- 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 ...
随机推荐
- 二叉查找树BST----java实现
二叉查找树BST----java实现 1.二叉查找树简单介绍 二叉查找树 ...
- java怎样将一个List传入Oracle存储过程
java怎样将一个List传入Oracle存储过程.样例例如以下: 数据库端建一个PL/SQL的数组. CREATE OR REPLACE TYPE tables_array AS VARRAY(10 ...
- JavaScript replace 回调函数用法
params.query = params.query.replace(/[\+\-\&\|\!\(\)\{\}\[\]\^\"\~\*\?\:\\]/g,function(a){ ...
- PyInstaller把.py转为.exe
http://www.pyinstaller.org/ http://blog.csdn.net/hmy1106/article/details/45151409 python pyinstaller ...
- 应用程序之Xib自定义Cell
效果展示 结构分析 代码实现 一.效果展示 二.结构分析 1⃣️首先我们让我们的控制器不再继承UIViewController,而是继承UITableViewController.这样就直接遵守了de ...
- 【Sprint3冲刺之前】TD学生助手测试用例
项目名称 TDzhushou 项目承担部门 骐骥之队 完成日期 2014/5/29 历史版本: 版本/状态 作者 参与者 起止日期 备注 TDzhushou1.1 解凤娇 骐骥之队 5/3-5/7 2 ...
- 小白学习python之路(二):安装开发工具
引言 上一章我们安装配置了python3.7,这一章我们安装python的开发工具,我用的pycharm2019 安装 工具连接:https://u20538204.ctfile.com/fs/205 ...
- 启动Eclipse时,启不起来JVM terminated. Exit code=-1
启动Eclipse时,启不起来JVM terminated. Exit code=-1 出现错误了,不知道什么原因原本好好的Eclipse,今天早上出问题了,启动不起来还抛出JVM terminate ...
- 去掉activity默认动画效果的方法
非常多手机都会自带一些Activity切换动画,项目中假设我们须要禁用掉系统Activity切换的动画.能够使用例如以下方法: 一.重写Activity的Them中的windowAnimationSt ...
- HTTP状态码介绍详细
HTTP协议中几个状态码的含义:1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明 100 (继续) 请求者应当继续提出请求. 服务器返回此代码表示已收到请求的第一部分, ...