Java Basic Learning Path (12) linked list

Source: Internet
Author: User

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

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.