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
- Allocate memory first, save new node
- Apply head from scratch and start chasing a check until you find the last node.
- Set the footer node address portion to the address of the new node
- 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.
- Allocating memory space
- Point the new node to the head
- 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