Java implementation of simple one-way linked list [inner class]

Article catalog

Source code implementation

package cn.Link;
interface ILink<E>
{
    public void add(E e);//Linked list data increase
    public int count();//Get the number of linked list elements
    public boolean isEmpty();//Empty set judgment
    public Object[] toArray();//Return linked list data
    public E get(int index);//Get data by index
    public void set(int index,E data);//Modify linked list data
    public boolean contains(E data);//Data content query
    public void remove(E data);//Delete linked list data: 1. Delete root node, 2. Delete child node
    public void clean();//Clear linked list data
}
class LinkImple<E> implements ILink<E>
{
    private int count;
    private Node root;
    private int foot;
    private Object [] returnData;

    private class Node
    {
        private E data;
        private Node next;
        public Node(E data){//Node storage data operation
            this.data=data;
        }
        public void addNode(Node newNode){//Node stores next node operation
            if(this.next==null){//When the memory of the next node of the current object is empty, the incoming node object is assigned to the memory of the next node of the current object
                this.next=newNode;
            }else{//When the memory of the next node of the current object is not empty, recursive call is used to save the node object
                this.next.addNode(newNode);
            }
        }
        public void toArrayNode(){
            LinkImple.this.returnData[LinkImple.this.foot++]=this.data;
            if(this.next!=null){
                this.next.toArrayNode();
            }
        }
        public E getNode(int index){
            if(LinkImple.this.foot++ == index){
                return this.data;
            }else{
                return this.next.getNode(index);//Return the data obtained here to the calling function with a return
            }
        }
        public void setNode(int index,E data){
            if(LinkImple.this.foot++ == index){
                this.data=data;
            }else{
                this.next.setNode(index,data);
            }
        }
        public boolean containsNode(E data){
            if(data.equals(this.data)){
                return true;
            }else if(this.next==null){
                return false;
            }else{
                return this.next.containsNode(data);
            }
        }
        public void removeNode(Node previous,E data){//Delete child nodes
            if(data.equals(this.data)){
                previous.next=this.next;
            }else if(this.next!=null){
                this.next.removeNode(this,data);
            }
        }
    }

    public void add(E e){
        if(e==null){
            return ;
        }
        Node newNode=new Node(e);
        if(this.root==null){
            root=newNode;
        }else{
            this.root.addNode(newNode);
        }
        count++;
    }
    public int count(){
        return this.count;
    }
    public boolean isEmpty(){
        //return this.root==null;
        return this.count==0;
    }
    public Object[] toArray(){
        if(this.isEmpty()){
            return null;
        }
        this.foot=0;
        this.returnData=new Object[this.count];
        this.root.toArrayNode();
        return returnData;
    }
    public E get(int index){
        if(index>=count){
            return null;
        }
        this.foot=0;
        return this.root.getNode(index);
    }
    public void set(int index,E data){
        if(index>=count){
            return ;
        }
        this.foot=0;
        this.root.setNode(index,data);
    }
    public boolean contains(E data){
        if(data==null){
            return false;
        }
        return this.root.containsNode(data);
    }
    public void remove(E data){//Delete root node
        if(data==null){
            return ;
        }
        if(data.equals(this.root.data)){
            this.root=this.root.next;
        }else{
            this.root.removeNode(this.root,data);
        }
        count--;
    }
    public void clean(){
        this.root=null;
        count=0;
    }
}
public class Link
{
    public static void main(String agrs[]){
        ILink <String>link=new LinkImple <String> ();
        System.out.println("Initialization data:"+link.count()+"\t Is it an empty set"+link.isEmpty());
        link.add("hello");
        link.add("world");
        link.add("!");
        System.out.println("Result data:"+link.count()+"\t Is it an empty set"+link.isEmpty());
        Object[] result=link.toArray();
        for(Object temp:result){
            System.out.println(temp);
        }
        System.out.println("---------------------Get linked list elements according to index--------------------");
        System.out.println(link.get(1));
        System.out.println("---------------------Modify linked list elements according to index--------------------");
        link.set(1,"WORLD");
        System.out.println(link.get(1));
        System.out.println("---------------------Judge whether there is corresponding linked list element according to array disk--------------------");
        System.out.println(link.contains("WORLD"));
        System.out.println("---------------------Delete corresponding linked list elements according to automatic content--------------------");
        link.remove("WORLD");
        Object[] resultB=link.toArray();
        for(Object temp:resultB){
            System.out.println(temp);
        }
        System.out.println("---------------------Empty list elements--------------------");
        link.clean();
        System.out.println(link.isEmpty());
    }
}

Operation results

Initialization data:0	 Is it an empty settrue
//Result data:3	 Is it an empty setfalse
hello
world
!
---------------------Get linked list elements according to index--------------------
world
---------------------Modify linked list elements according to index--------------------
WORLD
---------------------Judge whether there is corresponding linked list element according to array disk--------------------
true
---------------------Delete corresponding linked list elements according to automatic content--------------------
hello
!
---------------------Empty list elements--------------------
true

Process finished with exit code 0

Source code analysis

[ILink interface]: unifies the list operation specification, and exposes the list operation method while hiding the operation details.
[LinkImple]: used to implement the methods in ILink, in which there is a private internal class Node class, through which Node information is saved. In the internal class, there are also methods called by the external class LinkImple, such as addNode,toArrayNode,getNode,setNode,removeNode,containsNode These methods all follow the idea of recursion, through the Node object and the Next object of Node, recursively find the condition satisfied state, and realize the specific functions. These methods are used by the implementation methods of the external class LinkImple.
[add method implementation]: after removing the empty data from the external class, a Node class object is created to determine whether the root Node root is empty, and the empty object is assigned to the root. Otherwise, the internal class's addNode method uses the recursive form to find the empty Node and assign a value.
Count method implementation: there is a count variable in the external class. When a data is added, one is added. In the external class count method, only the count value needs to be returned.
[isEmpty method implementation]: judge whether the count data is 0.
[toArray method]: in the external class, there is an array of Object type called returnData[]. In the external class toArray method, the isEmpty method is first judged to be not empty. After the toArray in the internal class is used to fill in the data one by one, the external class only needs to return returnData.
[get method implementation]: when the input index is less than the length of the linked list, call the getNode method in the internal class to recursively find the node at the index location and return its data.
[set method implementation]: similar to get method
[contains method implementation]: when the input data is not empty, use the containsNode method in the inner class to recursively find the same node as the input data. Found return true.
[remove method implementation]: it can be divided into deleting the root node and deleting the child node. The remove method in the external class is compared with the data of the root node first. If it is the same, you can use the root.next Assign to root to delete the root node. When this node is not the same as the previous node and returned to the removeNode method in the internal class, the next of this node is assigned to the next of the previous node to complete the deletion of this node by recursion to the node with the same data. Note that the external class should reduce the count data by one
[clean method implementation]: assign null data to root.

Tags: less

Posted on Mon, 15 Jun 2020 23:19:09 -0400 by valoukh