Java data structures and algorithms------linear table (linked list structure)

Source: Internet
Author: User

1  PackageiYou.neugle.list;2 3  Public classMychainlist<t> {4     //add an element to the tail of the list5      PublicNode<t> AddEnd (node<t>Head, T data) {6node<t> node =NewNode<t>();7Node.data =data;8         if(Head = =NULL) {9Head =node;Ten             returnhead; One         } ASearchend (head). Next =node; -         returnhead; -     } the  -     PrivateNode<t> Searchend (node<t>head) { -         if(Head.next = =NULL) { -             returnhead; +         } -         returnsearchend (head.next); +     } A  at     //adding elements to the list header -      PublicNode<t> AddHead (node<t>Head, T data) { -node<t> node =NewNode<t>(); -Node.data =data; -node<t> nodeCopy = deepcopy (head,NewNode<t>()); -Node.next =nodeCopy; in  -Head.data =Node.data; toHead.next =Node.next; +         returnhead; -     } the  *     PrivateNode<t> deepcopy (node<t> head, node<t>node) { $         if(Head! =NULL) {Panax NotoginsengNode.data =Head.data; -Node.next =Head.next; the}Else { +             return NULL; A         } the deepcopy (Head.next, node.next); +         returnnode; -     } $  $     //inserting data into a linked list -      PublicNode<t> Insert (node<t>head, t key, T data) { -         if(Head.next = =NULL) { the             return NULL; -         }Wuyi  the         if(Head.data.equals (key)) { -             returnAddHead (head, data); Wu         } -  About         if(Head.next.data.equals (key)) { $node<t> node =NewNode<t>(); -Node.data =data; -Node.next =Head.next; -Head.next =node; A             returnhead; +         } the Insert (Head.next, key, data); -         returnhead; $     } the  the     //to delete data from a linked list the      PublicNode<t> Delete (node<t>head, T key) { the         if(Head.next = =NULL) { -             return NULL; in         } the  the         if(Head.data.equals (key)) { AboutHead.data =Head.next.data; theHead.next =Head.next.next; the             returnhead; the         } +  -         if(Head.next.data.equals (key)) { theHead.next =Head.next.next;Bayi             returnhead; the         } the Delete (Head.next, key); -         returnhead; -     } the  the     //find nodes by keyword the      Public voidSearch (node<t>head, T key) { the         if(Head.data.equals (key)) { -SYSTEM.OUT.PRINTLN ("The query node is the head node, the value of the nodes is:" +key); the             return; the         } the          while(Head.next! =NULL) {94             if(Head.next.data.equals (key)) { theSystem.out.print ("Query node:" + key + ","); theSystem.out.print ("Query node predecessor node is:" + Head.data + ","); the                 if(Head.next.next! =NULL) {98SYSTEM.OUT.PRINTLN ("Query node post node is:" +head.next.next.data); About}Else { -SYSTEM.OUT.PRINTLN ("Query node is tail node!"));101                 }102                 return;103             }104Head =Head.next; the         }106System.out.println ("No this node!"));107     }108 109     //Get the chain table length the      Public intSizeOf (node<t>head) {111         //If there are no nodes, return-1 the         if(Head = =NULL) {113             return-1; the         } the         intn = 1; the          while(Head.next! =NULL) {117n++;118Head =Head.next;119         } -         returnN;121     }122 123      Public voidPrint (node<t>head) {124          while(Head! =NULL) { the System.out.println (head.data);126Head =Head.next;127         } -     }129}

Java data structures and algorithms------linear table (linked list structure)

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.