Java implementation of circular linked list

Source: Internet
Author: User

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

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.