博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法(Algorithms)第4版 练习 1.3.25 1.3.24
阅读量:6771 次
发布时间:2019-06-26

本文共 4077 字,大约阅读时间需要 13 分钟。

代码实现:

//1.3.24    /**     * remove the node following the node x     * (and does nothing if the argument or the next field in the argument node is null)     *      * @param x the given node     */    public static 
void removeAfter(Node
x) { if(x == null || x.next == null) return; Node
current = x.next; x.next = null; while(current != null) { Node
temp = current.next; current.next = null; current = temp; } } //1.3.25 /** * insert the second node after the first on its list. * and does nothing if either argument is null. * * @param first the first node * @param second the second node to be inserted after the first */ public static
void insertAfter(Node
first, Node
second) { if(first == null || second == null) return; second.next = first.next; first.next = second; }

 测试用例:

package com.qiusongde.linkedlist;import com.qiusongde.linkedlist.LinkedList.Node;import edu.princeton.cs.algs4.StdOut;import edu.princeton.cs.algs4.StdRandom;public class Exercise1325 {    public static void main(String[] args) {                int number = StdRandom.uniform(10) + 1;//1~10 Node        StdOut.println("The size of array is:" + number);        Node
[] nodes = (Node
[]) new Node[number];//initialize array for(int i = 0; i < number; i++) { nodes[i] = new Node
();//should initialize Node again nodes[i].item = i + 1; nodes[i].next = null; if(i > 0) { LinkedList.insertAfter(nodes[i-1], nodes[i]); StdOut.printf("insert node %s After %s success\n", nodes[i].item, nodes[i-1].item); } } LinkedList.insertAfter(nodes[0], null); StdOut.printf("insert a null node After node %s success\n", nodes[0].item); LinkedList.insertAfter(null, nodes[0]); StdOut.printf("insert node %s After a null node success\n", nodes[0].item); LinkedList
list = new LinkedList
(nodes[0]); StdOut.println("The list whose first node is:" + nodes[0].item); StdOut.println(list); int number2 = number -1; StdOut.printf("The nodes after %s will be removed\n", nodes[number2].item); LinkedList.removeAfter(nodes[number2]); StdOut.println("Remove sucess after node" + nodes[number2].item); StdOut.println("The list whose first node is:" + nodes[0].item); StdOut.println(list); number2 = number /2; StdOut.printf("The nodes after %s will be removed\n", nodes[number2].item); LinkedList.removeAfter(nodes[number2]); StdOut.println("Remove sucess after node" + nodes[number2].item); StdOut.println("The list whose first node is:" + nodes[0].item); StdOut.println(list); StdOut.printf("The nodes after a null node will be removed\n"); LinkedList.removeAfter(null); StdOut.println("Remove sucess after a null node"); StdOut.println("The list whose first node is:" + nodes[0].item); StdOut.println(list); }}

 

结果输出:

The size of array is:7insert node 2 After 1 successinsert node 3 After 2 successinsert node 4 After 3 successinsert node 5 After 4 successinsert node 6 After 5 successinsert node 7 After 6 successinsert a null node After node 1 successinsert node 1 After a null node successThe list whose first node is:11 2 3 4 5 6 7 The nodes after 7 will be removedRemove sucess after node7The list whose first node is:11 2 3 4 5 6 7 The nodes after 4 will be removedRemove sucess after node4The list whose first node is:11 2 3 4 The nodes after a null node will be removedRemove sucess after a null nodeThe list whose first node is:11 2 3 4

 

转载于:https://www.cnblogs.com/songdechiu/p/6512531.html

你可能感兴趣的文章
DOM4J介绍与代码示例
查看>>
linux 命令集合1
查看>>
数据管理DMBOK总体介绍
查看>>
揭开.NET程序保护的秘密
查看>>
工控项目开发框架介绍
查看>>
Exchange企业实战技巧(23)用户邮箱数据的导入导出
查看>>
用FileZilla Server架设开源FTP服务器:看图入门
查看>>
Silverlight Toolkit预览Part 1:介绍
查看>>
旷视Face++与西交大成立AI联合实验室,郑南宁孙剑再续师徒缘
查看>>
《深入理解计算机系统(修订版)》读感
查看>>
Linux下如何使用虚拟用户增加FTP的安全性
查看>>
为了学习WinCE嵌入式编程,又开始学起了MFC
查看>>
[RHEL5企业级Linux服务攻略]--第2季 Samba服务全攻略
查看>>
JSP和ASP.NET到底谁才是未来的主宰
查看>>
Goldengate双向复制配置
查看>>
CSS魔法堂:盒子模型简介
查看>>
忘记电商 刘强东和京东的“如意算盘”
查看>>
Oracle多实例的配置方法
查看>>
浅谈hibernate+入门实例
查看>>
Linux on Power 上的调试工具和技术
查看>>