The Java implementation single-linked list has been described earlier: http://www.cnblogs.com/lixiaolun/p/4643886.html
In fact, the main difference between the two is how to determine whether the end of the list:
In a single-linked list
while (temp.next!=null) {temp=temp.next;}
In the circular link list
while (Temp.next!=header) {temp=temp.next;}
Here is the code for the Loop list and the test code:
The code for the Loop list:
Package Circularlinkedlist;public class Circularlinkedlist {class Element{public Object value=null;private Element Next =null;} Private Element Header = null;//Header node/** * Initialize linked list * */void initlist () {header = new Element (); header.value=null;header.next=he Ader;} /** * Insert list * */void insertlist (Object o) {element e=new element (); E.value=o;if (Header.next==header)//Insert element for the first time { Header.next=e;e.next=header;} else//is not the first time the element {//temp reference is inserted in the stack, both the temp and header references point to the element object of the new in the heap initlist () Element temp = Header;while (temp.next!= header)//search for the last element {Temp=temp.next;} temp.next=e;e.next=header;//the last node of the new insertion points to the head node}}/** * Delete the list I element in the list * */void deletelist (Object o) {element temp =header;while (Temp.next!=header) {//Determines if the next node of the node to which temp is currently pointing is the node to be deleted if (Temp.next.value.equals (o)) {temp.next=temp.next.next;//Delete node}else{temp= Temp.next;//temp "Pointer" moves back}}}/** * Gets the element of the list I position * */element getelement (int i) {if (i<=0 | | i>size ()) { System.out.println ("Get the list in the wrong place!") return null "); return null;} Else{int count = 0; element element = new element (); Element TEMP = Header; while (Temp.next!=header) {count++;if (count==i) {element.value=temp.next.value;} Temp=temp.next;} return element;}} /** * List Length * */int size () {Element temp = Header;int Size=0;while (temp.next!=header) {size++;temp=temp.next;} return size;} /** * Determine if an element exists in the linked list * */boolean iscontain (Object o) {element temp =header;while (Temp.next!=header) {if ( Temp.next.value.equals (o)) {return true;} Temp=temp.next;} return false;} /** * Print List * */void print () {System.out.print ("Print List:"); Element temp =header;while (temp.next!=header) {temp=temp.next; System.out.print (temp.value+ "\ t");} System.out.println ();}}
Test code:
Package Circularlinkedlist;public class Circularlinkedlistmain {public static void main (string[] args) { Circularlinkedlist cllist = new Circularlinkedlist (); Cllist.initlist (); cllist.insertlist (1); cllist.insertlist (2); Cllist.insertlist (3); cllist.insertlist (4); cllist.insertlist (5); Cllist.print (); SYSTEM.OUT.PRINTLN ("List length:" +cllist.size ()); cllist.deletelist (1); cllist.deletelist (5); Cllist.print (); System.out.println ("The 1th element value is:" +cllist.getelement (1). value); System.out.println ("The 2nd element value is:" +cllist.getelement (2). value); System.out.println ("The 3rd element value is:" +cllist.getelement (3). value); System.out.println (Cllist.iscontain (2)); System.out.println (Cllist.iscontain (6));//system.out.println (Cllist.iscontain (5));}}
Java implementation of circular linked list