zoukankan      html  css  js  c++  java
  • 算法-第四版-练习1.3.25解答

    问题

    编写一个方法insertAfter(),接受两个链表结点作为参数,将第二结点插入链表并使之成为第一个结点的后续结点(如果两个参数为空则什么也不做)。

    解决思路

    插入过程保持后续连接正常。

    代码

        public void insertAfter(Node<Item> pos, Node<Item> node)
        {
            if (node == null || pos == node) 
                return;
            node.next = pos.next;
            pos.next = node;
        }

    测试代码:

    /**
     * Description : 
     * Author      : mn@furzoom.com
     * Date        : Oct 25, 2016 10:07:50 AM
     * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved.
     */
    package com.furzoom.lab.algs.ch103;
    
    /**
     * ClassName    : E10325 <br>
     * Function     : TODO ADD FUNCTION. <br>
     * date         : Oct 25, 2016 10:07:50 AM <br>
     * 
     * @version 
     */
    public class E10325
    {
        public static void main(String[] args)
        {
            LinkList<String> ll = new LinkList<String>();
            ll.append("a");
            ll.append("B");
            ll.append("c");
            ll.append("D");
            ll.append("e");
            
            ll.printList();
            
            Node<String> node = null;
            Node<String> pos = null;
            ll.insertAfter(pos, node);
            System.out.println("insertAfter(null, null) : ");
            ll.printList();
            
            pos = ll.search("e");
            ll.insertAfter(pos, node);
            System.out.println("insertAfter("e", null): ");
            ll.printList();
            
            node = new Node<String>();
            node.item = "F";
            ll.insertAfter(pos, node);
            System.out.println("insertAfter("e", "F"): ");
            ll.printList();
            
            pos = ll.search("c");
            node = new Node<String>();
            node.item = "X";
            ll.insertAfter(pos, node);
            System.out.println("insertAfter("c", "X"): ");
            ll.printList();
            
        }
    }
    

    结果:

    a
    B
    c
    D
    e
    insertAfter(null, null) : 
    a
    B
    c
    D
    e
    insertAfter("e", null): 
    a
    B
    c
    D
    e
    insertAfter("e", "F"): 
    a
    B
    c
    D
    e
    F
    insertAfter("c", "X"): 
    a
    B
    c
    X
    D
    e
    F
    



    算法-第四版-1.3 背包、队列和栈-习题索引汇总

    算法-第四版习题索引汇总


    作者:马 岩Furzoom) (http://www.cnblogs.com/furzoom/
    版权声明:本文的版权归作者与博客园共同所有。转载时请在明显地方注明本文的详细链接,未经作者同意请不要删除此段声明,感谢您为保护知识产权做出的贡献。
  • 相关阅读:
    oracle 自动备份
    oracle 常用操作语句
    数据库创建及使用注意事项
    oracle 导入 导出 备份
    http://blog.sina.com.cn/s/blog_5fc8b3810100iw9n.html
    利用普通工具编译的第一个Servlet
    对java:comp/env的研究(转)
    MyEclipse配置tomcat、jdk和发布第一个web项目
    构建 SSH 框架(转)
    Java Project和Web Project
  • 原文地址:https://www.cnblogs.com/furzoom/p/7710177.html
Copyright © 2011-2022 走看看