Java implementation of single-linked list

Source: Internet
Author: User


Linked list is a set of arbitrary storage units to store linear table data units, linked list consists of two parts: node (data field), pointer field. Access to the entire list must start from the beginning of the pointer, the head pointer points to the first node, and the last node points to null. There is no pointer concept in Java, but there are references in Java, and references can be used instead of pointers.

Here is an example of what I wrote:

Package Com.test.linked;public class Newlinklist {public class node{private Object data;private int index;private Node NEX T;public Node (int index,object data) {This.index=index;this.data=data;} Public String toString () {return index+ "...." +data.tostring ();}} Private Node header;private int size=0;/** * Insert in Header * @param index * @param o */public void insertfirst (int index,object o) { if (header==null) {header=new Node (0,0);} Node node=new node (index,o); node.next=header.next;header.next=node;size++;} /** * Insert at a specific location (inserted after that position) * @param key * @param index * @param o */public void insertbykey (int key,int index,object o) {Node P=findlist (key); Node node=new node (index,o); node.next=p.next;p.next=node;size++; System.out.println ("Insertbykey finished");} /** * Delete */public void Deletefirst () {if (size>0) {header.next=header.next.next;size--;} in Header Else{system.out.println ("The linked list is empty!") ");}} public int size () {return size;} /** * Delete specific location elements * @param key */public void Deletebykey (int key) {Node previous=header; Node P=header;while (p.Index!=key) {if (p.next==null) {System.out.println ("can ' t find!");} Else{previous=p;p=p.next;}} Previous.next=previous.next.next; System.out.println (previous);} /** * list showing */public void Displaylist () {Node p=header; System.out.println ("List header: Last), while (p.next!=null) {p=p.next; System.out.println (P.tostring ());}} /** * Through key lookup, extensible, if key is the same multiple Node found object by equals method * @param key * @return */public node findlist (int key) {node P=header;while (p . Index!=key) {if (p.next==null) {System.out.println ("can ' t find!"); return null;} Else{p=p.next;}} System.out.println ("Find this Node"); return p;} public static void Main (string[] args) {newlinklist list=new newlinklist (); List.insertfirst (1, "the most"); List.insertfirst (2, "No.2"); List.insertfirst (3, "No.3"); List.displaylist ();//list.deletefirst ();// System.out.println (List.findlist (2). toString ());//list.displaylist (); List.insertbykey (2, 6, "I m be Inserted");// List.displaylist (); List.deletebykey (6); List.displaylist ();}}
Output Result:

List (header: Last) 3....no.32 .... No.21 .... No.1find this Nodeinsertbykey finished2 .... No.2list (header: Last) 3....no.32 .... No.21 .... The



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