Java implementation single-linked list

Source: Internet
Author: User

The previous introduction to how Java implements the sequential chain list: http://www.cnblogs.com/lixiaolun/p/4643664.html

Next, we began to learn the Java implementation of a single-linked list.

Single-linked list class

Package Linklist;public class Linklist {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=nu ll;} /** * Insert list * */void insertlist (Object o) {element e=new element (); E.value=o;if (header.next==null)//Insert element for first time {header.next= e;} 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!= NULL)//Find last element {Temp=temp.next;} Temp.next=e;}} /** * Delete List i elements * */void deletelist (Object o) {element temp =header;while (temp.next!=null) {// Determines whether the next node of the node currently pointed to by temp 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 link list in the wrong place! return null "); return null;} Else{int count = 0; element element = new element (); Element temp = header;while (temp.next!=null) {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!=null) {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!=null) {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!=null) {temp=temp.next; System.out.print (temp.value+ "\ t");} System.out.println ();}}

Test class

Package Linklist;public class Linklistmain {public static void main (string[] args) {linklist llist = new linklist (); llist. Initlist (); llist.insertlist (1); llist.insertlist (2); llist.insertlist (3); llist.insertlist (4); Llist.insertlist (5) ; Llist.print (); llist.deletelist (2); Llist.print (); SYSTEM.OUT.PRINTLN ("Linked list length:" +llist.size ()); System.out.println ("The 1th element value is:" +llist.getelement (1). value); System.out.println ("The 2nd element value is:" +llist.getelement (2). value); System.out.println ("The 3rd element value is:" +llist.getelement (3). value); System.out.println ("The 4th element value is:" +llist.getelement (4). value); System.out.println (Llist.iscontain (2)); System.out.println (Llist.iscontain (6)); System.out.println (Llist.iscontain (5));}}

  

Java implementation single-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.