Data Structure-Java Implementation of Single Linked List

Links to the original text: https://blog.csdn.net/coder150806/article/details/82425219

In the past, I only used arrays. Arrays have fixed length. They have shortcomings. They can't be changed at will. Link lists can insert and delete a large number of elements.

Next, let me introduce the single list.

What is a single linked list?

Each node of a linked list contains only one pointer field, which is called a single linked list (that is, each node of the linked list has only one pointer to the direct successor node, and the pointer of the tail node is null).

The node structure is as follows:

Next, let me give you a simple list of single links that store integers.
I. A single linked list of integers is divided into three parts.
1. Nodes
2. Interface
3. Link List

1. Node class, which mainly defines node data, pointer attributes and Node class construction method in the interface, facilitates how to add data in the linked list.
Here's the code for the Node class of the node interface

//Node class
public class Node {
	//data
	int data;
	//Pointer, or the next node
	Node next;
	//Create a parameter-free structure for easy initialization
	public Node(){
	}
	//Create a parameterized construction method to pass data to the node
	public Node(int data){
	    this.data=data;
	}
}

2. Interface class, we define some methods to delete integers, add integers, get integers, modify, insert, and get integers in the interface.  
In the linked list, you can interface to call some methods; let me give the code below.

//Interface myList class, which defines the method of storing linked list data
public interface myList {
    //storage
    public void add(int e);
    //Insert
    public void insert(int e,int index);
    //delete
    public void delete(int index);
    //Get elements
    public int get(int index);
    //modify
    public void modify(int e,int index);
    //Get the number of elements
    public int getSize();
}

3. By inheriting the interface myList in the linked list class, we can call the methods defined in the interface by creating objects, such as deleting, getting integers, etc.
There are three important attributes in the list, which are the head node, the tail node and the number of elements.
1. Head Node: The pointer next of the header node is pointing to the first node.
2. Tail Node: Last Node
3. Number of elements

Here is the code to write the linked list class

Now let me add a few pictures to show the deletion. In fact, the deletion of integers is similar to the insertion of integers.

The following picture is about deleting the integer method. To delete the integer is to assign the data value of the node where the index subscript is located to 0, the next pointer of the node is empty, and then point the pointer of index-1 to the node of index+1.

Here's the code for deleting integers from linked list classes

    //Delete integers
    public void delete(int index) {
       // TODO Auto-generated method stub
       checkindex(index);

       Node node = head.next;

       //Find the subscript index
       for (int i = 0; i < index; i++) {
           node = node.next;
       }

       //Find the subscript index-1
       Node node1 = head.next;
       for (int i = 0; i < index - 1; i++) {
           node1 = node1.next;
       }

       if (index != 0) {//Assume index is not equal to 0; not the first node
           if (index == size - 1) {//The hypothesis is the last one.
               node1.next = null;    
           }
            else{//Assuming it's not the last one
               node1.next = null;
               node1.next = node.next;
           }
            //Set the node's pointer to be empty and the node's value to be empty
           node.next = null;
           node.data = 0;
       }else{//Suppose index equals 0.
           head.next = null;
           head.next = node.next;
           node.next = null;
           node.data = 0;
       }
       size--;
   }
}

The following picture is the case of inserting an integer method. Inserting an integer is to empty the pointer of node 1 where index-1 is located, then point the pointer of index-1 node to the node to be inserted, and then point the pointer of node 2 to the node node to be inserted.  

    //Insert integers
    public void insert(int e, int index) {//The parameter index denotes the subscript of the node's location
        // TODO Auto-generated method stub
       checkindex(index);
       Node node = head.next;
        //Find the subscript index
       for (int i = 0; i < index; i++) {
           node = node.next;
       }
        //Find the subscript index-1
       Node node1 = head.next;
       for (int i = 0; i < index - 1; i++) {
           node1 = node1.next;
       }
        //Create a new node for the node to insert
       Node node2 = new Node(e);
       if (index == 0) {//If the insertion location is the first node
           head.next = null;
           head.next = node2;
       } else {
           node1.next = null;
           node1.next = node2;
       }
           node2.next = node;
       size++;
 
   }

Next is the whole code of Link class in single linked list. The others get the number of elements, modify integers, store integers, and get integers all in it.

public class Link implements myList{
    //Head node, tail node
    Node head,tail;
    //Number of elements
    int size;
    public Link(){
        head = new Node();
        tail = head;
        size = 0;
    }
 
    @Override
    //Storage integers
    public void add(int e) {//The formal parameter e represents the integer to be added
        // TODO Auto-generated method stub
        //Create a new node
        Node node = new Node(e);
        //Set the next node of the endpoint to be the new node
        tail.next=node;
        //Set the endpoint to a new node
        tail=node;
        //Number of elements plus one
        size++;
    }
 
    @Override
    //Insert integers
    public void insert(int e, int index) {//The parameter index denotes the subscript of the node's location
        // TODO Auto-generated method stub
        checkindex(index);
        Node node=head.next;
        //Find the subscript index
        for(int i=0;i<index;i++){
            node=node.next;
        }
        //Find the subscript index-1
        Node node1=head.next;
        for(int i=0;i<index-1;i++){
            node1=node1.next;
        }
        //Create a new node for the node to insert
        Node node2=new Node(e);
        if(index==0){//If the insertion location is the first node
            head.next=null;
            head.next=node2;
        }else{
            node1.next=null;
            node1.next=node2;
        }
            node2.next=node;
        size++;
 
    }
 
    @Override
    //Delete integers
    public void delete(int index) {
        // TODO Auto-generated method stub
        checkindex(index);
        Node node=head.next;
        //Find the subscript index
        for(int i=0;i<index;i++){
            node=node.next;
        }
        //Find the subscript index-1
        Node node1=head.next;
        for(int i=0;i<index-1;i++){
            node1=node1.next;
        }
        if(index!=0){//Assume index is not equal to 0; not the first node
            if(index==size-1){//The hypothesis is the last one.
                node1.next=null;    
            }
            else {//Assuming it's not the last one
                node1.next=null;
                node1.next=node.next;
            }
            //Set the node's pointer to be empty and the node's value to be empty
            node.next=null;
            node.data=0;
        }
        else{//Suppose index equals 0.
            head.next=null;
            head.next=node.next;
            node.next=null;
            node.data =0;
        }
        size--;
    }
 
    @Override
    //Get integers
    public int get(int index) {
        // TODO Auto-generated method stub
        //Find the following table index
        Node node=head.next;
        //Starting from the first node, look backwards in turn
        for(int i=0;i<index;i++){
            node = node.next;
        }
 
        return node.data;
    }
 
    @Override
    //Modify integers
    public void modify(int e, int index) {
        // TODO Auto-generated method stub
        //Find the following table index
        Node node=head.next;
        //Starting from the first node, look backwards in turn
        for(int i=0;i<index;i++){
            node = node.next;
        }
        Node node1=new Node(e);
        node.data=e;
    }
 
    @Override
    //Get the number of elements
    public int getSize() {
        // TODO Auto-generated method stub
        return size;
    }
    //Check whether index value is qualified
    public void checkindex(int index){
        if(index<0||index>size){
            throw new IndexOutOfBoundsException("The parameters do not meet the requirements.");
        }
    }
    public static void main(String [] args){
        Link link = new Link();
        //Add data
        link.add(12);
        link.add(34);
        link.add(56);
        link.add(78);
        link.add(90);
        //Traversal list
        for(int i=0;i<link.getSize();i++){
            //Take out the element
            int e=link.get(i);
            //Output element
            System.out.println("The first"+i+"The elements are:"+e);
 
        }
    }
 
}

 

Posted by Hannes2k on Fri, 30 Aug 2019 01:30:53 -0700