Circular chain list: On the basis of a single linked list, the first corresponding to form a ring, take the first and the end of the term, the time complexity of 0 (1)
Meaning: The sense of not feeling too much, mainly in any position to be able to loop through the whole table,
Code
Package math;import java.util.hashmap;import java.util.list;import java.util.map;import java.util.Random;import Java.util.uuid;import Math. Singlelinklist.obj;/** * @author bin * DATE:15/1/5 * desc: Implementation of the circular link list because of the single-linked list, this is simply written down, mainly single-linked list closed into a ring * Let the last memory address point to the first item*/ Public classLooplinklist { PublicList L; Public intCurrentlen;//Current Length PublicMap m; PublicString address;// Public classobj{PrivateString address; PrivateObject obj; PublicObj (String str,object o) { This. Address =str; This. obj =o; } PublicObj () {}} Publiclooplinklist () { This. Currentlen =0; //Initialize, memory address This. Address = This. Getuuid (); //Initialize Mapm =NewHashMap (); } Public voidCreatlist (inti) { This. Check (i); Random R=NewRandom (); String TempStr=NewString (); String UUID=NewString (); intrint; for(intj =0; J < I; J + +) {rint= R.nextint ( +); System. out. println (rint); UUID= This. Getuuid (); OBJ o=NewObj (UUID, rint); if(J = =0){ //the current recordm.put (address, O); }Else if(j = =-1) {o=NewOBJ (Address,rint); M.put (TempStr, O); }Else{m.put (TEMPSTR, O); } tempstr=uuid; Currentlen= currentlen+1; } } PublicObject Getele (inti) { This. Check (i); intStart =0; OBJ o=NewOBJ (); if(Currentlen <1 ){ Throw NewRuntimeException ("Unreasonable parameters"); }Else if(I >Currentlen) { Throw NewRuntimeException ("Out of bounds error"); }Else{ while(Start <= I-1) { if(Start = =0) {o= (looplinklist.obj) m.Get(address); }Else{ //System.out.println (M.get (o.address). toString ());o = (looplinklist.obj) m.Get(o.address); } Start++; } returno; } } Public voidLoop (Obj o) {intCount =0; System. out. println ("-------------------"); System. out. println (O.obj); while(Count < currentlen-1) {o= (OBJ) m.Get(o.address); System. out. println (O.obj); Count++; } } Public Static voidMain (string[] args) {looplinklist L=Newlooplinklist (); L.creatlist (Ten); Looplinklist Loop=Newlooplinklist (); OBJ o= Loop.NewOBJ (); o= (OBJ) L.getele (4); L.loop (o); } Public voidCheckinti) { if(i<=0){ Throw NewRuntimeException ("not valid parameters"); } } //to generate unique address information PublicString Getuuid () {uuid UUID=Uuid.randomuuid (); returnuuid.tostring (); }}
Results
- - at to at - 6 - to -------------------6 - - - at
Loop linked list of algorithms