×

算法练习(37): 链表的增删查改3(1.3.24-1.3.27)

96
算法之路
2017.11.03 22:18* 字数 550

本系列博客习题来自《算法(第四版)》,算是本人的读书笔记,如果有人在读这本书的,欢迎大家多多交流。为了方便讨论,本人新建了一个微信群(算法交流),想要加入的,请添加我的微信号:zhujinhui207407 谢谢。另外,本人的个人博客 http://www.kyson.cn 也在不停的更新中,欢迎一起讨论

算法(第4版)

知识点

  • 链表节点增删查改

题目

1.3.24 编写一个方法removeAfter(),接受一个链表结点作为参数并删除该结点的后续结点(如果参数结点或参数结点的后续结点为空则什么也不做)。


1.3.24 Write a method removeAfter() that takes a linked-list Node as argument and removes the node following the given one (and does nothing if the argument or the next field in the argument node is null).

答案

public class LinkedListExecise4<Item> {

    private static class Node<Item> {
        Node next;
        Item item;
        
        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "item:"+item;
        }
    }

    public Node<Item> removeAfter(Node node, Node first) {
        if (first == null) {
            return null;
        }

        Node current = first;
        while (current != null) {
            if (current.item.equals(node.item)) 
            {
                if (current.next != null) 
                {
                    current.next = current.next.next;
                    return first;
                } else {
                    return first;
                }
            }
            current = current.next;
        }

        return null;
    }

    /**
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /**
         * 创建链表
         * */
        Node<String> first = new Node<String>();
        Node<String> second = new Node<String>();
        Node<String> third = new Node<String>();
        Node<String> forth = new Node<String>();
        Node<String> fifth = new Node<String>();
        first.item = "我的";
        first.next = second;
        second.item = "名字";
        second.next = third;
        third.item = "叫";
        third.next = forth;
        forth.item = "顶级程序员不穿女装";
        forth.next = fifth;
        fifth.item = "微博:https://m.weibo.cn/p/1005056186766482";
        fifth.next = null;

        LinkedListExecise4<String> linkedListExercise4 = new LinkedListExecise4<String>();
        Node targetNode = first;
        System.out.println("即将移除节点:"+targetNode+"之后的节点");
        Node resultNode = linkedListExercise4.removeAfter(targetNode, first);

        System.out.println("新链表:\n-------");
        Node current2 = resultNode;
        while (current2.next != null) {
            System.out.println(current2.item);
            current2 = current2.next;
        }
        System.out.println(current2.item);
        System.out.println("-------");
    }
}

代码索引

LinkedListExecise4.java

题目

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


1.3.25 1.3.25 Write a method insertAfter() that takes two linked-list Node arguments and inserts the second after the first on its list (and does nothing if either argument is null).

答案

public class LinkedListExecise5<Item> {

    private static class Node<Item> {
        Node next;
        Item item;

        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "item:" + item;
        }
    }

    public Node<Item> insertAfter(Node<Item> targetNode, Node<Item> node,
            Node<Item> first) {
        if (targetNode == null || node == null) {
            return first;
        }
        Node<Item> current = first;
        while (current != null) {
            if (current.equals(targetNode)) {
                Node<Item> t = current.next;
                current.next = node;
                node.next = t;
                return first;
            }
            current = current.next;
        }

        return null;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /**
         * 创建链表
         * */
        Node<String> first = new Node<String>();
        Node<String> second = new Node<String>();
        Node<String> third = new Node<String>();
        Node<String> forth = new Node<String>();
        Node<String> fifth = new Node<String>();
        first.item = "我的";
        first.next = second;
        second.item = "名字";
        second.next = third;
        third.item = "叫";
        third.next = forth;
        forth.item = "顶级程序员不穿女装";
        forth.next = fifth;
        fifth.item = "微博:https://m.weibo.cn/p/1005056186766482";
        fifth.next = null;

        LinkedListExecise5<String> linkedListExercise5 = new LinkedListExecise5<String>();
        Node targetNode = second;
        System.out.println("即将移除节点:" + targetNode + "之后的节点");
        Node<String> insertedNode = new Node<String>();
        insertedNode.item = "天天开心笑哈哈";
        Node resultNode = linkedListExercise5.insertAfter(targetNode,
                insertedNode, first);

        System.out.println("新链表:\n-------");
        Node current2 = resultNode;
        while (current2.next != null) {
            System.out.println(current2.item);
            current2 = current2.next;
        }
        System.out.println(current2.item);
        System.out.println("-------");
    }

}

代码索引

LinkedListExecise5.java

题目

1.3.26 编写一个方法remove(),接受一个链表和一个字符串key作为参数,删除链表中所有item域为key的结点。


1.3.26 Write a method remove() that takes a linkedlist and a string key as arguments and removes all of the nodes in the list that have key as its item field.

答案

public class LinkedListExecise6 {

    private static class Node {
        Node next;
        String item;

        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "item:" + item;
        }
    }
    
    public Node remove(Node first, String key) {
        Node newFirst = new Node();
        newFirst.next = first;
        
        Node current = newFirst;
        while (current.next != null) {
            if (current.next.item.equals(key)) {
                current.next = current.next.next;
            }else {
                current = current.next;
            }
        }
        
        return newFirst.next;
    
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        /**
         * 创建链表
         * */
        Node first = new Node();
        Node second = new Node();
        Node third = new Node();
        Node forth = new Node();
        Node fifth = new Node();
        first.item = "我的";
        first.next = second;
        second.item = "名字";
        second.next = third;
        third.item = "叫";
        third.next = forth;
        forth.item = "顶级程序员不穿女装";
        forth.next = fifth;
        fifth.item = "微博:https://m.weibo.cn/p/1005056186766482";
        fifth.next = null;

        LinkedListExecise6 linkedListExercise6 = new LinkedListExecise6();
        Node resultNode = linkedListExercise6.remove(first, "我的");

        System.out.println("新链表:\n-------");
        Node current2 = resultNode;
        while (current2.next != null) {
            System.out.println(current2.item);
            current2 = current2.next;
        }
        System.out.println(current2.item);
        System.out.println("-------");

    }
}

代码索引

LinkedListExecise6.java

题目

1.3.27 编写一个方法max(),接受一个链表的首结点作为参数,返回链表中键最大的节点的值。假设所有键均为正整数,如果链表为空则返回0。


1.3.27 Write a method max() that takes a reference to the first node in a linkedlist as argument and returns the value of the maximum key in the list. Assume that all keys are positive integers, and return 0 if the list is empty.

答案

public class LinkedListExecise7 {

    private static class Node {
        Node next;
        Integer item;

        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "item:" + item;
        }

    }

    public Integer max(Node first) {
        if (null == first) {
            return 0;
        }

        if (first.next == null) {
            return first.item;
        }

        Node current = first;
        Integer max = current.item;
        while (current != null) {
            if (current.item > max) {
                max = current.item;
            }
            current = current.next;
        }

        return max;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /**
         * 创建链表
         * */
        Node first = new Node();
        Node second = new Node();
        Node third = new Node();
        Node forth = new Node();
        Node fifth = new Node();
        first.item = 1;
        second.item = 3;
        third.item = 999;
        forth.item = 33;
        fifth.item = 21;

        first.next = second;
        second.next = third;
        third.next = forth;
        forth.next = fifth;
        fifth.next = null;

        System.out.println("原链表:\n-------");
        Node current1 = first;
        while (current1.next != null) {
            System.out.println(current1.item);
            current1 = current1.next;
        }
        System.out.println(current1.item);
        System.out.println("-------");

        System.out.println("正在求最大值...");
        LinkedListExecise7 linkedListExercise7 = new LinkedListExecise7();
        Integer result = linkedListExercise7.max(first);
        System.out.println("result:" + result);
    }
}

代码索引

LinkedListExecise7.java

日记本
Web note ad 1