首页 > 代码库 > 算法(Algorithms)第4版 练习 1.3.25 1.3.24

算法(Algorithms)第4版 练习 1.3.25 1.3.24

代码实现:

    //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 <T> void removeAfter(Node<T> x) {
        
        if(x == null || x.next == null) 
            return;
        
        Node<T> current = x.next;
        x.next = null;
        
        while(current != null) {
            Node<T> 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 <T> void insertAfter(Node<T> first, Node<T> 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<Integer>[] nodes = (Node<Integer>[]) new Node[number];//initialize array 
        
        for(int i = 0; i < number; i++) {
            nodes[i] = new Node<Integer>();//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<Integer> list = new LinkedList<Integer>(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:7
insert node 2 After 1 success
insert node 3 After 2 success
insert node 4 After 3 success
insert node 5 After 4 success
insert node 6 After 5 success
insert node 7 After 6 success
insert a null node After node 1 success
insert node 1 After a null node success
The list whose first node is:1
1 2 3 4 5 6 7 
The nodes after 7 will be removed
Remove sucess after node7
The list whose first node is:1
1 2 3 4 5 6 7 
The nodes after 4 will be removed
Remove sucess after node4
The list whose first node is:1
1 2 3 4 
The nodes after a null node will be removed
Remove sucess after a null node
The list whose first node is:1
1 2 3 4 

 

算法(Algorithms)第4版 练习 1.3.25 1.3.24