Java implementation chain List simulation LinkedList class

Source: Internet
Author: User

LinkedList class underlying data structure

Simulation:

1  PackageCollection;2 3  Public classMylinkedlist {4 Node first;5 Node last;6     Private intsize;7      Publicmylinkedlist () {8         9     }Ten     /* One * Add a new object A      * */ -      Public BooleanAdd (Object obj) { -Node node =NewNode (); the         if(First = =NULL){ -First =node; -Last =node; -Node.prev =NULL; +Node.next =NULL; -Node.obj =obj; +}Else{ ANode.prev = Last;//Next Association previous atNode.next =NULL; -Node.obj =obj; -Last.next = node;//Previous Association Next -Last =node; -         } -size++; in         return true; -     } to     /* + * Add a new element at index as the element corresponding to index -      * */ the      Public BooleanAddintindex, Object obj) { * Checkbound (index); $Node node = This. First;Panax NotoginsengNode nodetemp =NewNode (); -         if(Index = = 0){ theNodetemp.obj =obj; +Nodetemp.prev =NULL; ANodetemp.next = This. First; the              This. First.prev =nodetemp; +              This. First =nodetemp; -size++; $}Else if(Index = = size-1){ $ Add (obj); -size++; -}Else{ theNodetemp.obj =obj; -              for(inti = 0; I < index; i++)Wuyinode =Node.next; theNodetemp.next =node; -Nodetemp.prev =Node.prev; WuNode.prev.next =nodetemp; -Node.prev =nodetemp; Aboutsize++; $         } -         return true; -     } -     /* A * Get the number of elements +      * */ the      Public intsize () { -         return  This. Size; $     } the     /* the * Get elements by index the      * */ the      PublicObject Get (intindex) { - Checkbound (index); inNode node = This. First; the          for(inti = 0; I < index; i++) thenode =Node.next; About         if(Node! =NULL) the             returnnode.obj; the         Else the             return NULL;  +     } -     /* the * Remove the element at indexBayi      * */ the      Public BooleanRemoveintindex) { the Checkbound (index); -Node node = This. First; -         if(Index = = 0){ the              This. First = This. First.next; thesize--; the}Else if(Index = = size-1){ the              for(inti = 0; I < index; i++) -node =Node.next; theNode.prev.next =NULL; thesize--; the}Else{94              for(inti = 0; I < index; i++) thenode =Node.next; theNode.prev.next =Node.next; theNode.next.prev =Node.prev;98size--; About         } -         return true;101     }102     /*103 * Removing obj objects104      * */ the      Public BooleanRemove (Object obj) {106 Remove (indexOf (obj));107         return true;108     } 109     /* the * Retrieve object obj, return index111      * */ the     Private intindexOf (Object obj) {113Node node = This. First; the          for(inti = 0; I < This. size; i++){ the             if(node.obj.equals (obj)) the                 returni;117node =Node.next;118         }119         return-1; -     }121     /*122 * Reverse Search123      * */124      Public intlastIndexof (Object obj) { theNode node = This. Last;126          for(inti = 0; I < This. size; i++){127             if(node.obj.equals (obj)) -                 returnSize-i-1;129node =Node.prev; the         }131         return-1; the     }133     /*134 * Check Border135      * */136     Private voidCheckbound (intindex) {137         if(Index < 0 | | index > This. Size)138             Try {139                 Throw NewException (); $}Catch(Exception e) {141                 //TODO auto-generated Catch block142 e.printstacktrace ();143             }144     }145 }146 /*147 * Node class148  * */149 classnode{ Max Node prev;151 Node Next; the Object obj;153      PublicNode () {154         155     }156}

Java implementation chain List simulation LinkedList class

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.