Algorithm (algorithms) 4th edition Practice 1.3.25 1.3.24

Source: Internet
Author: User

Code implementation:

    //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) * *@paramx the given node*/     Public Static<T>voidRemoveafter (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 in its list.     * and does nothing if either argument is null. *      * @paramFirst the first node *@paramsecond the second node to being inserted after the first*/     Public Static<T>voidInsertAfter (node<t> First, node<t>second) {                if(First = =NULL|| Second = =NULL)             return; Second.next=First.next; First.next=second; }

Test Case:

 Packagecom.qiusongde.linkedlist;ImportCom.qiusongde.linkedlist.LinkedList.Node;ImportEdu.princeton.cs.algs4.StdOut;ImportEdu.princeton.cs.algs4.StdRandom; Public classExercise1325 { Public Static voidMain (string[] args) {intNumber = Stdrandom.uniform (10) + 1;//1~10 NodeStdout.println ("The size of the array is:" +Number ); Node<integer>[] nodes = (node<integer>[])NewNode[number];//Initialize Array                 for(inti = 0; I < number; i++) {Nodes[i]=NewNode<integer> ();//should initialize Node againNodes[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 =NewLinkedlist<integer> (nodes[0]); Stdout.println ("The list whose first node is:" + nodes[0].item);                STDOUT.PRINTLN (list); intNumber2 = Number-1; stdout.printf ("The nodes after%s would 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 would 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 would 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); }}

Result output:

The size of array is:7Insert Node2 after 1successinsert Node3 after 2successinsert Node4 after 3successinsert Node5 after 4successinsert Node6 after 5successinsert Node7 after 6Successinsert aNULLNode after Node 1successinsert Node1 after aNULLnode Successthe list whose first node is:11 2 3 4 5 6 7The nodes after7'll be removedremove sucess after node7the list whose first node is:11 2 3 4 5 6 7The nodes after4'll be removedremove sucess after node4the list whose first node is:11 2 3 4The nodes after aNULLnode'll be Removedremove sucess after aNULLnodethe list whose first node is:11 2 3 4

Algorithm (algorithms) 4th edition Practice 1.3.25 1.3.24

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.