代码实现:

    //1.3.27
/**
* return the value of the maximum key in the list
*
* @param list the linked list of Integer
*
* @return return the maximum key in the list
*/
public static int max(LinkedList<Integer> list) { if(list.first == null)
return 0; int max = 0;
for(int val : list) {
if(val > max)
max = val;
} return max;
} //1.3.28
/**
* return the value of the maximum key in the list by recursion
*
* @param list the linked list of Integer
*
* @return return the maximum key in the list
*/
public static int maxByRecursion(LinkedList<Integer> list) { if(list.first == null)
return 0; int first = list.first.item;//first item
list.first = list.first.next;//remove first item in the list
int max = maxByRecursion(list);//calculate the maximum value of the new list if(first > max)
return first;
else
return max;
}

测试用例:

package com.qiusongde.linkedlist;

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class Exercise1327 { public static void main(String[] args) { LinkedList<Integer> list = new LinkedList<Integer>(); while(!StdIn.isEmpty()) {
int val = StdIn.readInt();
list.insertAtBeginning(val);
StdOut.println("insertAtBeginning success: " + val);
StdOut.println(list);
} int max = LinkedList.max(list);
StdOut.println("The maximum key is:" + max); max = LinkedList.maxByRecursion(list);
StdOut.println("The maximum key is:" + max + "(By Recursion)");
} }

测试数据1:

insertAtBeginning success: 10
10
insertAtBeginning success: 25
25 10
insertAtBeginning success: 30
30 25 10
insertAtBeginning success: 100
100 30 25 10
insertAtBeginning success: 51
51 100 30 25 10
insertAtBeginning success: 26
26 51 100 30 25 10
insertAtBeginning success: 69
69 26 51 100 30 25 10
insertAtBeginning success: 6
6 69 26 51 100 30 25 10
insertAtBeginning success: 32
32 6 69 26 51 100 30 25 10
insertAtBeginning success: 78
78 32 6 69 26 51 100 30 25 10
insertAtBeginning success: 90
90 78 32 6 69 26 51 100 30 25 10
The maximum key is:100
The maximum key is:100(By Recursion)

测试数据2:

insertAtBeginning success: 90
90
insertAtBeginning success: 78
78 90
The maximum key is:90
The maximum key is:90(By Recursion)

测试数据3:

insertAtBeginning success: 90
90
The maximum key is:90
The maximum key is:90(By Recursion)

测试数据4(输入为空):

The maximum key is:0
The maximum key is:0(By Recursion)

算法(Algorithms)第4版 练习 1.3.27 1.3.28的更多相关文章

  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搭建的教程 ...

随机推荐

  1. Notification(二)——PendingIntent的flag导致数据同样的问题

    MainActivity例如以下: package cc.cu; import android.os.Bundle; import android.view.View; import android. ...

  2. 在VS2010中如何添加MSCOMM控件,实现串口通讯

      参考文章:http://wenku.baidu.com/link?url=MLGQojaxyHnEgngEAXG8oPnISuM9SVaDzNTvg0oTSrrJkMXIR_6MR3cO_Vnh- ...

  3. 如何在VS2010中添加ActiveX控件及使用方法

    方法1: 1.首先在在项目上面右击添加类,如下图所示: 2.点击添加ActiveX控件中的MFC类 3.找到需要添加的ActiveX类. 4.点击完成即可. 5.此时转到资源视图,打开如下视图.可能工 ...

  4. 设置windows时间开机同步方法

    本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 适用场景: 主板电池 ...

  5. (八)jQuery中的事件

    1.加载DOM在常规的JavaScript中,使用window.onload方法:而在jQuery中,使用$(document).ready(function(){ })方法.window.onloa ...

  6. Time倒计时

    commitTimeDate = new Date("2016/11/9 10:02:40").getTime() + 24*60*60*1000;//截止时间 myDate = ...

  7. Java 加解密技术系列之 DES

    序 前几篇文章讲的都是单向加密算法.当中涉及到了 BASE64.MD5.SHA.HMAC 等几个比較常见的加解密算法. 这篇文章,以及后面几篇.打算介绍几个对称加密算法.比方:DES.3DES(Tri ...

  8. 转:SATA协议简介

    SATA协议简介 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/FA99999/article/details/70738724 1.概述 本文档主 ...

  9. 【强网杯2018】逆向hide

    这是事后才做出来的,网上没有找到现成的writeup,所以在这里记录一下 UPX加壳,而且linux下upx -d无法解,也无法gdb/ida attach 因为是64位,所以没有pushad,只能挨 ...

  10. ubantu 彻底卸载mysql

    卸载mysql 第一步 1 sudo apt-get autoremove --purge mysql-server-5.0 2 sudo apt-get remove mysql-server 3 ...