Java code implements the linked list structure, and has realized adding, deleting, and checking whether a node is included, and printing all nodes.

First of all, the general structure of our code is as follows: create a Node manager class, use an internal class to create an internal class, and use yourself as a Node. The code structure is like this

This is to show the structure of the code with pictures. It belongs to the chain list structure, which is layer by layer, ring by ring. A circle represents an object in the code.


The code ability is not very good. The modification and insertion functions are not implemented. Please give me some advice and communicate more
Some of them are not perfect. I will improve them if I have a chance

Here is the code display:

package play;

/**
 * ░░░░░░░░░░░░░░░░░░░░░░░░▄░░
 * ░░░░░░░░░▐█░░░░░░░░░░░▄▀▒▌░
 * ░░░░░░░░▐▀▒█░░░░░░░░▄▀▒▒▒▐
 * ░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐
 * ░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐
 * ░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌
 * ░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒
 * ░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐
 * ░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄
 * ░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒
 * ▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒
 * @author: Xuan Xuan
 * Linked list is a common basic data structure, which is a linear list,
 * But instead of storing the data in the first place, it's in each
 * Pointer to the next node in the node
 */
public class LinkedList {
    public static void main (String[] args){
        NodeManager nm = new NodeManager();
        /**
         * User manual:
         * nm.add(); Add feature: implemented
         * nm.print();Output function: implemented
         *  nm.del();Delete function: implemented
         *  nm.find():Query contains: implemented
         *  Modification function: not implemented
         *  Insert function: not implemented
         */

        nm.add(4);
        nm.add(3);
        nm.add(2);
        nm.add(1);

        nm.print();
        System.out.println(nm.find(5));

    }
}
//Node manager: managing nodes
class NodeManager{

    //There is a next in root, and next is a Node
    private Node root;//Root node
    //Add (implemented)
    public void add(int data){
        //Determine whether the root node is not empty
        if(root == null){
            root = new Node(data);
        }else{
            root.addNode(data);
        }
    }
    //Delete (implemented)
    public void del(int data){
        if(root.getDate() == data){
            root = root.next;
        }else{
            root.delNode(data);
        }
    }
    //Print all data (implemented)
    public void print(){

        if(root != null){
            System.out.print(root.getDate()+"-->");
            root.echoNode();
            System.out.println();
        }
    }
    //Query whether a node exists (implemented)
    public boolean find(int data){
        if(root == null){return false;}//If the following node is empty, return false directly
        if(root.getDate() == data){
            return true;
        }else {
            return root.findNode(data);
        }
    }
    //modify
    public void update(int oldData , int newDate){

    }
    //insert
    public void insert(int index,int date){

    }


    //Internal storage of a node class (Node) to store data
    private class Node{
        private Node next;// Take the current type as an attribute
        private int date;//Stored data

        public Node(int date) {
            this.date = date;
        }

        //get and set methods of data
        public int getDate() {
            return date;
        }
        public void setDate(int date) {
            this.date = date;
        }

        //Add node (implemented)
        public void addNode(int data){
            //This: whoever calls the method represents who, so this represents root
            //Determine whether there is next data. If there is no new Node class added directly
            //If there is no data, the next addNode method in root will be used, which is also a linked list
            //Reasons for slow addition and deletion
            if(this.next == null){
                this.next = new Node(data);
            }else {
                this.next.addNode(data);
            }
        }
        //Delete node (implemented)
        public void delNode(int data){
            //Judge is not equal to empty and compare again
            if(this.next !=null){
                if(this.next.date == data){
                    this.next = this.next.next;
                }else {
                    this.next.delNode(data);
                }
            }
        }
        //Output all nodes (implemented)
        public void echoNode(){
            if(this.next != null){
                System.out.print(this.next.date+"-->");
                this.next.echoNode();
            }
        }
        //Query whether a node exists
        public boolean findNode(int date){
            if(this.next != null){
                if(this.next.date == date){
                    return true;
                }else {
                    this.next.findNode(date);
                }
            }
            return false;
        }
        //Modify node
        public void updateNode(int oldDate , int Date){

        }
        //Insertion node
        public void insert(int index , int date){

        }
    }
}

Tags: NodeManager Attribute

Posted on Tue, 05 Nov 2019 11:46:09 -0800 by 1042