Java data structure: linked list

Source: Internet
Author: User

Concept: linked lists can make an ordered or unordered list, the content is usually stored in the memory of scattered locations, there are nodes, each node structure is the same, the node is divided into data domain and chain domain, the data domain storage content, the chain is the next node pointer.

Basic method implementation:

New node:

To delete a node:

Print node:

The array implements a single-linked list:

Create node class: Node.java

 PackageLinkpackage;/** * @authorZH * Node model*/ Public classNode {String text;    Node Next; //head Node builder     PublicNode () { This. Text = "";  This. Next =NULL; }    //Node Builder     PublicNode (String text) { This. Text =text;  This. Next =Next; } @Override PublicString toString () {return"node{" + "text=" "+ text +" \ "+", next= "+ Next + '} '; }}

Create a linked list class: Link.java

 PackageLinkpackage; Public classLink {//Head knot Point    PrivateNode Head; //construct linked list     PublicLink () {head=NewNode (); }    //Adding nodes     Public voidAddNode (String text) {//Create the nodes you want to addNode node =NewNode (text); //Create a temporary nodeNode temp =Head;  while(Temp.next! =NULL){            //change the loop conditiontemp =Temp.next; } Temp.next=node; }    //Calculate Length     Public intsize () {intLen = 0; Node Temp=Head;  while(Temp.next! =NULL) {len++; Temp=Temp.next; }        returnLen; }    /*** Print linked list node*/     Public voidDisplayLink () {//Set Temporary nodeNode temp =Head; //Start Traversal         while(Temp.next! =NULL){            //Output NodeSystem.out.print (temp.next.text+ "===>"); //Start next nodetemp =Temp.next; }    }    /*** Show Delete node*/     Public voidDelnode (String data) {//Temporary NodeNode temp =Head; //start looping through linked lists         while(Temp.next! =NULL){            //Find Delete node            if(temp.next.text.equals (data)) {//point the Pointer field of the list to the next nodeTemp.next =Temp.next.next; //Exit Loop                 Break; }            //when the condition is not met, the next node is foundtemp =Temp.next; }    }    /*** Show Find node*/     PublicNode FindNode (String nodeName) {//Set Temporary nodeNode temp =Head; //Create an empty nodeNode data =NULL; //traversing a linked list         while(Temp.next! =NULL){            //find criteria to match            if(Temp.next.text.equals (nodeName)) {//Assign Valuedata =Temp.next; //Exit Loop                 Break; }            //condition does not conform to the next node operation, that is, changing the loop conditiontemp =Temp.next; }        //return Query Results        returndata; }    /*** Show reverse single linked list*/     Publicnode Reverse (node node) {if(node = =NULL|| Node.next = =NULL){            returnnode; } Node Renode=reverse (Node.next); Node.next.next=node; Node.next=NULL; returnRenode; }     Public Static voidMain (String args[]) {link link=NewLink (); Link.addnode ("Test linked list node added one"); Link.addnode ("Test List node added two"); Link.addnode ("Test List node added three"); Link.addnode ("Test List node added four"); Link.addnode ("Test List node added five");        System.out.println (Link.head); System.out.println ("Linked list length:" +link.size ());        Link.displaylink ();        System.out.println (); Link.delnode ("Test List node added three"); System.out.println ("Linked list length:" +link.size ());        Link.displaylink ();        System.out.println (); Node Data= Link.findnode ("Test List node added four");        System.out.print (data); Node Node=Link.reverse (Link.head);  while(Node.next! =NULL) {System.out.print (Node.text+ "===>"); Node=Node.next; }    }}

Java data structure: 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.