Java implementation linked list structure detail code

Source: Internet
Author: User

First, data preparation

1. Defining nodes

2. Define the linked list

1. Data section

2. Node section

class Data              // node type {  String key;  String name;    int Age ;                }     class Cltype                 // define linked list structure {data nodedata  =new  data (); Cltype NextNode;}

Second, append node

    1. Allocate memory first, save new node
    2. Apply head from scratch and start chasing a check until you find the last node.
    3. Set the footer node address portion to the address of the new node
    4. Place the address portion of the new node to null
cltype claddend (cltype head,data nodedata) {Cltype node,temp; if((node=NewCltype () = =NULL) {System.out.println ("Request Memory Failed"); return NULL;//failed to request memory}      Else{node.nodedata=Nodedata; Node.nextnode=NULL; if(head==NULL) {Head=node; returnHead; } temp=Head;  while(temp!=NULL) {Temp=Temp.nextnode; Find the end of the list} Temp.nextnode=node; returnhead;}} 

Third, insert the head node.

    1. Allocating memory space
    2. Point the new node to the head
    3. Make head point to new node
cltype Claddfirst (DATA nodedata,cltype head) {      cltype node;       if ((node=new cltype ()) = =null)         {             returnnull;
         }
Else { Node.nodedata=nodedata; Node.nextnode=head; Head=node; return head; }}

Iv. Finding nodes

cltype Clfindnode (cltype head,string key) {         Cltype temp;          =head;           while (temp!=null) {          if(temp.nodadata==key)           return  Temp ;           Temp=temp.nextnode;         }           return NULL ;}

V. Inserting nodes

cltype Clinsentnode (cltype head,string findkey,data nodedata) {Cltype node,nodetemp; if((node=NewCltype ()) = =NULL) {System.out.println ("Application Memory failed!" "); return NULL; } node.nodadata=Nodedata; Nodetemp= (Head,findkey);//find the corresponding node       if(nodetemp!=NULL)//If a node is found{Node.nextnode=Nodetemp.nextnode; Nodetemp.nextnode=node; }        Else{System.out.println ("The correct insertion position was not found!" "); }        returnhead;}

Vi. Deleting nodes

intCldeletenode (Cltype head;      String key) {Cktype node,temp; Node=Head; Temp=Head;  while(temp!=NULL)      {            if(Temp.nodeData.key.compareTo (Key) ==0) {Node.nextnode=Temp.nextnode; Temp=NULL; return1; } ELAS {Note=temp; Temp=Temp.nextnode; }           }       return0;} 

Vii. calculation of the chain table length

int Cllength (Cltype head) {     Cltype temp;      = head;      int length=0;       while (temp!=null)     {             length+ +;             Temp=temp.nextnode;      }       return length;}

Eight. Show all nodes

void Clallnode (Cltype head) {       Cltype temp;       DATA  nodedata;        = head;       System.out.println ("The current list has a +cllength (head) +" node.) The linked list data is as follows: ");              while (temp!=null)       {           Nodedata=temp.nodedata;           System.out.println ("+nodedata.key+nodedata.name+nodedata.age");           Temp=temp.nextnode;       }}

Java implementation linked list structure detail code

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.