Java data Structure--the implementation of circular chain list

Source: Internet
Author: User

1 //single-Link table node class2  Public classnode<t> {//single-linked table node class, t specifies the element type of the node3 4      PublicT data;//data fields, saving data elements5      PublicNode<t> Next;//Address field, successor node reference6 7     //construct node, data specifies the element, next specifies the successor node .8      PublicNode (T data, node<t>next) {9          This. data =data;Ten          This. Next =Next; One     } A  -      PublicNode () { -          This(NULL,NULL); the     } -  -     //returns the string corresponding to the node . -      PublicString toString () { +         return  This. data.tostring (); -     } +  A     //compares two node values for equality, overriding the Equals (obj) method of the object class at      Public Booleanequals (Object obj) { -         returnobj = = This|| ObjinstanceofNode && This. Data.equals (((node<t>) (obj). data); -     } -}

1 //cyclic single-link list class for linear table interface2  Public classLooplinklist<t> {3 4     //head Pointer, point to the head node of the circular single-linked list5      PublicNode<t>head;6 7     //default construction method, constructs an empty loop single-linked list8      Publiclooplinklist () {9          This. Head =NewNode<t>();Ten          This. Head.next = This. Head;////Creating a head node One     } A  -     //determine if the circular single-linked list is empty -      Public BooleanIsEmpty () { the         return  This. Head.next = = This. Head; -     } -  -     //constructs a single-linked list from multiple objects in an element array. Construct single-linked list with tail insertion +      Publiclooplinklist (t[] element) { -          This();//Create an empty single-linked list with only the head node +node<t> rear = This. Head;//rear points to the last node of the single-linked list A          for(inti = 0; i < element.length; i++) {//if Element==null, throws an empty object exception at             //if element.length==0, construct empty linked list -Rear.next =NewNode<t> (Element[i], This. Head);//tail Insert, after creating the node link into the rear node -Rear = Rear.next;//rear point to new link end node -         } -     } -  in     //returns the loop single-linked list length, single-linked list traversal algorithm, O (n) -      Public intLength () { to         inti = 0; +          for(Node<t> p = This. Head.next; P! = This. Head; p =p.next) -i++; the         returni; *     } $ Panax Notoginseng     //returns the element I (≥0) and returns NULL,O (n) if i<0 or greater than the length of the table -      PublicT Get (inti) { the         if(I >= 0) { +Node<t> p = This. Head.next; A              for(intj = 0; P! = This. Head && J < i; J + +) thep =P.next; +             if(P! = This. Head) -                 returnP.data;//p points to the first node $         } $         return NULL;//when i<0 or greater than the length of the table -     } -  the     //sets the value of the element I (≥0) to X. If i<0 or greater than the length of the table, throw an ordinal out of bounds, if x==null, do not operate. O (n) -      Public voidSetintI, T x) {Wuyi         if(x = =NULL)return;//cannot set empty object theNode<t> p = This. Head.next; -          for(intj = 0; P! = This. Head && J < i; J + +) Wup =P.next; -         if(i >= 0 && P! = This. Head) AboutP.data = x;//p points to the first node $         Else Throw NewIndexoutofboundsexception (i + "");//throws an ordinal out of bounds exception -     } -}

1   Public Static voidMain (String args[]) {2 3Integer[] Array=NewInteger[]{12,25,55,78,99,-17};4looplinklist<integer> linklist =NewLooplinklist<integer>(array);5 6         intLength=linklist.length ();7         intA=linklist.get (5);8 9System.out.println ("The linked list is empty:" +linklist.isempty ());TenSYSTEM.OUT.PRINTLN ("Linked list length is:" +length); OneSYSTEM.OUT.PRINTLN ("Get data at the specified location is:" +a); A  -          for(inti=0;i<length;i++) - System.out.println (Linklist.get (i)); the  -Linklist.set (1,200); -  -          for(inti=0;i<length;i++) + System.out.println (Linklist.get (i)); -}

Java data Structure--the implementation of circular chain list

Related Article

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.