Example explanation-unidirectional list (2)

Source: Internet
Author: User

In-depth list operations

The inner class will be used to complete the operation of the list!

One, the improvement of the list

A simple one-way list has been implemented before, but it is certainly not possible to handle the relationships of each node manually.

So it's best to encapsulate the operation of the node here. This makes the user's operation very convenient.

Suppose you have the following node operations: Adding data, finding data, and deleting data.

It is particularly emphasized that if you want to delete a node, you can modify the previous node directly.

Add nodes: This is the last increment of the node.

Find a node: It is a recursive way to find.

Delete node: Just change the reference delivery address.

In program development, two scenarios must be considered:

1) The first time execution, when the first execution, its root node does not exist, you need to set the first data to become the root node.

classlink{//the completion class of the linked list    classnode{//Save each node here for immediate definition as an inner class        PrivateString data;//Save the contents of a node        PrivateNode Next;//Save Next node         PublicNode (String data) { This. data = data;//setting node content by constructing methods        }         Public voidAdd (Node newNode) {//Add a node to the appropriate location            if( This. next==NULL){//if the next node is empty, set the new node to the next location                 This. Next =NewNode; }Else{//If it is  not empty, then there are other nodes, which can not be added, you need to continue to find the next empty node, AutoComplete is added at the end of the list.                  This. Next.add (NewNode); }        }         Public voidprint () {System.out.print ( This. Data + "\ T");//Output Node Content            if( This. next!=NULL){//there's the next element that needs to continue to output                 This. Next.print ();//The next node continues to call print            }        }         Public BooleanSearch (String data) {//methods for internal search            if(Data.equals ( This. Data)) {//determine if the input data is consistent with the current node's data                return true ; }Else{//continue judging downward                if( This. next!=NULL){//the next node, if it exists, continues to find                    return  This. Next.search (data);//returns the next query result}Else{                    return false;//returns False if no content is equal after all nodes have been queried                }            }        }         Public voidDelete (Node previous,string data) {if(Data.equals ( This. Data)) {//A matching node was foundPrevious.next = This. Next;//empty the current node}Else{                if( This. next!=NULL){//There is still the next node                     This. Next.delete ( This, data);//continue to find                }            }        }    }; PrivateNode Root;//there must be a root node in the linked list     Public voidAddNode (String data) {//Adding nodesNode NewNode =NewNode (data);//define a new node        if( This. root==NULL){//No root node             This. root = NewNode;//set the first node to a root node}Else{//is not the root node and is placed after the last node             This. Root.add (NewNode);//The location of this node is automatically arranged through node, and the add node is located starting at the root node.         }    }     Public voidPrintnode () {//output all the linked list contents        if( This. root!=NULL){//if the root element is not empty             This. Root.print ();//invoking output operations in the node class        }    }     Public BooleanContains (String name) {//determine if an element exists        return  This. Root.search (name);//call the Find method in the node class    }     Public voidDeletenode (String data) {//Delete a node        if( This. Contains (data)) {//determine if a node exists//be sure to determine if this element is now equal to the root element            if( This. Root.data.equals (data)) {//content is the root node                 This. root = This. Root.next;//Modify the root node to set the first node as the root node}Else{                 This.Root.next. Delete (Root,data);//The first node of the next node and the data are passed in , with the This.root.next call method, is the use of this current object in the calling method, as the initial judgment is not equal to the node to be deleted.             }        }    }}; Public classlinkdemo02{ Public Static voidMain (String args[]) {Link L=NewLink (); L.addnode ("A");//Adding nodesL.addnode ("B");//Adding nodesL.addnode ("C");//Adding nodesL.addnode ("D");//Adding nodesL.addnode ("E");//Adding nodesSystem.out.println ("======= ======== before deletion") ;        L.printnode (); //System.out.println (L.contains ("X"));L.deletenode ("C");//Delete a nodeL.deletenode ("D");//Delete a nodeL.deletenode ("A");//Delete a nodeSystem.out.println ("\n====== ========= after deletion") ;        L.printnode (); System.out.println ("\ n Query node:" + l.contains ("B")) ; }};

2) in order to view the entire list, a method for printing the linked list is defined here.

3) Find the node, must continue to use recursive operation to find operations.

4) Complete the deletion function, if you want to delete the node, it is definitely to change the content of the reference object of the node. However, make sure that this node exists before you delete it. Otherwise, it cannot be deleted.

5) Delete the node to determine whether it is the root node, if the root node, you need to modify his next for the root node.

6) To do the delete operation, you need to delete the node and the node in front of him to pass in the deletion method, because to modify the reference object, must be the existence of the previous node.

Summarize

1, the problem takes the list itself as an external class. Instead, the node acts as an inner class.

2, the knowledge points used here include recursion, the current object function of this.

3, because the increase node does not know the position of the first node, so need to start from the root node lookup, so the root node call to add methods, and then according to the next node to determine the situation to add.

4, this program is actually the extension application for reference. And in this program just realize the simplest one-way list function.

Example explanation-unidirectional list (2)

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.