Define the basic structure of a linked list:
classLink {//External Class//internal class, only for linked list class service Private classNode {//Defining node Classes PrivateString data;//the saved data PrivateNode Next;//Referential Relationships PublicNode (String data) { This. data =data; } PrivateNode Root;//defining the root node } }
1. Data increase public void add (data type, variable)
If you want to add data to the list, the link class should be responsible for generating the node object and maintaining the root node by the link class
All relationship matches are given to node class processing
classLink {//External Class PrivateNode Root;//defining the root node//internal class, only for linked list class service Private classNode {//Defining node Classes PrivateString data;//the saved data PrivateNode Next;//Referential Relationships PublicNode (String data) { This. data =data; } Public voidAddNode (Node newNode) {if( This. Next = =NULL) {//next bit empty, directly behind This. Next =NewNode; } Else{//next not empty, move back one judge again This. Next.addnode (NewNode); } } } Public voidAdd (String data) {if(Data = =NULL) { return; }; Node NewNode=NewNode (data); if( This. root = =NULL) { This. root =NewNode; } Else { This. Root.addnode (NewNode); } } } Public classTest1 { Public Static voidMain (String args[]) {link link=NewLink (); Link.add ("Hello"); Link.add ("World"); Link.add (NULL); }}
2. Get the saved node number public int size ()
1. Increment the Count property in the link class this. Count + + , which indicates that each increment of node is incremented by an int size () method, which returns the value of Count
3. Determine if the empty list is public boolean isEmpty ()
Two methods: 1. Determine if root is empty
2. Determine the amount of data (count)
Public Boolean IsEmpty () { returnthis. Count = = 0;}
4. Data Query public boolean contains (data type, variable)
Determine if a data exists
Java Basic Learning Path (12) linked list