Chained storage is based on java

Posted by shafiq2626 on Tue, 08 Mar 2022 20:21:09 +0100

1 linked list structure

The sequence table has the following disadvantages:

  • When inserting or deleting nodes, it is often necessary to move a large amount of data
  • If the table is large, it is sometimes difficult to allocate enough memory space, which may lead to memory allocation failure and unable to store

In order to overcome the shortcomings of linear list, linked list appears. Linked list is a structural form of dynamic storage and allocation, which can dynamically apply for the required memory units according to needs.

Form of linked list
Each node should contain:

  • The data part saves the actual data of the node
  • The address part stores the address value of the next node

2 advantages and disadvantages of linked list

2.1 advantages

The biggest advantage of the linked list structure is that there is no need for continuous storage between nodes. Users can use the new function to dynamically allocate the storage space of nodes. When deleting a node, assign null to the node to release the memory space occupied by it.

2.2 disadvantages

1 waste of storage space
2. For the access to the linked list, you can only find it one by one from the header, that is, find the first node through the head header reference, and then find the second node from the first node...., The access speed is relatively slow

2.3 summarize the advantages and disadvantages

Sequence table, class is array, query, fast modification, slow addition and deletion
Linked list, fast addition and deletion, slow query and modification

Precautions for use

When inserting a single node, first point the new node next to the successor node, and then point the predecessor node to the new node

3 function realization

3.1 preparation

  • 1 create a class to store information
public class Data1 { //Create a node to store data information
    String key;
    String name;
    int age;
}
  • 2. Create the data structure of linked list
public class CLType {
    Data1 nodeData = new Data1(); //Storage node
    CLType nextNode;// Store the address value of the next node
}

3.2 add nodes

//Additional node
    CLType CLAddEnd(CLType head,Data1 nodeData){

        /**
         * The data field of the head node does not store data, and the pointer field points to the next node
         * The pointer field of the tail node is null and does not store the address
         */

        CLType node,htemp;
        if ((node = new CLType()) == null){
            System.out.println("Failed to apply for memory!\n");
            return null;
        }else {
            //Indicates that the memory request is successful
            node.nodeData = nodeData;
            node.nextNode = null;

            if (head == null){ //The linked list is empty
                head = node;
                return head;
            }
            //Find the end of the linked list
            htemp = head; // Htemp refers to the current node and head refers to the previous node of htemp
            while (htemp.nextNode != null){
                htemp = htemp.nextNode;
            }
            htemp.nextNode = node;
            return head;
        }
    }

3.3 inserting nodes

/**Insert the head node, that is, the process of adding nodes at the head of the linked list. The steps are as follows
 * 1 Allocate memory space to ensure new nodes
 * 2 Make the new node point to the node pointed by the head reference head
 * 3 Make the header reference head point to the new node
 */
    CLType CLAddFirst(CLType head,Data1 nodeData){ //Header node and stored data
        CLType node;
        if ((node = new CLType()) == null){
            System.out.println("Failed to apply for memory!\n");
            return null;
        }else {
            node.nodeData = nodeData;
            node.nextNode = head;
            head = node;
            return head;
        }
    }

3.4 finding nodes

	//Find node
    CLType CLFindNode(CLType head,String key){
        CLType htemp;
        htemp = head; //Guarantee chain header reference
        while (htemp != null){
            if (htemp.nodeData.key.compareTo(key) == 0){
                return htemp;
            }
            htemp = htemp.nextNode;//Continue processing the value of the next node
        }
        return null;
    }

3.5 inserting nodes

    //Insert node
    CLType CLInsertNode(CLType head,String findKey,Data1 nodeData){

        CLType node,nodetemp;

        if ((node = new CLType()) == null){
            System.out.println("Failed to apply for memory!\n");
            return null;
        }

        //Save data in node
        node.nodeData = nodeData; // Save data in node

        nodetemp = CLFindNode(head,findKey); // Find inserted node values

        if (nodetemp != null){
            node.nextNode = nodetemp.nextNode; // When inserting a node, first point the pointer field of the new node to the data field of the next node

            nodetemp.nextNode = node; //Set the key node to point to the newly inserted node
        }else {
            System.out.println("The correct insertion position was not found!\n");
        }
        return head;
    }

3.6 deleting nodes

    //Delete node
    int CLDeleteNode(CLType head,String key){
        CLType node,htemp;

        htemp = head; //To delete a node
        node = head; // Keep the previous node of the node

        while (htemp != null){
            if (htemp.nodeData.key.compareTo(key) == 0){ // Represents the node to be deleted
                node.nextNode = htemp.nextNode; // Assign the pointer field of the node to be deleted to the pointer field of the previous node
                htemp = null; // Free memory
                return 1; // On behalf of success
            }else {
               //not found
                node = htemp;
                htemp = htemp.nextNode;
            }
        }
        return 0;
    }

3.7 calculate the length of the linked list

    //Calculate the length of the linked list
    int CLLength(CLType head){

        CLType htemp;
        int len = 0;
        htemp = head;

        while (htemp != null){
            len ++;
            htemp = htemp.nextNode;
        }
        return len;
    }

3.8 display all nodes

    //Show all nodes
    void CLAllNode(CLType head){
        CLType htemp;
        Data1 nodedata;
        htemp = head;
        System.out.println("Shared by current node"+CLLength(htemp)+"Nodes, displaying");
        while (htemp != null){

            nodedata = htemp.nodeData;
            System.out.println(nodedata.key+":"+nodedata.name+":"+nodedata.age);
            htemp = htemp.nextNode;
        }
    }

4. Whether the test function is realized

public class MyTest {
    public static void main(String[] args) {
        CLType node = null;
        CLType head = null;
        CLType CL = new CLType();

        String key, findkey;
        Scanner sc = new Scanner(System.in);
        System.out.println("To start the linked list test, first input the data in the linked list in the format of keyword, name and age");

        do {
            Data1 nodeData = new Data1();
            nodeData.key = sc.next();
            if (nodeData.key.equals("0")) {
                break; //If the keyword entered is 0, it means to exit the program
            } else {
                nodeData.name = sc.next();
                nodeData.age = sc.nextInt();
                head = CL.CLAddEnd(head, nodeData); // Add node at tail
            }
        } while (true);

        //Show all nodes
        CL.CLAllNode(head);

        //Insert node
        System.out.println("Please enter the keyword of the node to be inserted");
        findkey = sc.next();

        System.out.println("To insert the name of the node, please enter the age information");
        Data1 nodeData = new Data1();

        nodeData.key = sc.next();
        nodeData.name = sc.next();
        nodeData.age = sc.nextInt();

        head = CL.CLInsertNode(head, findkey, nodeData);

        //Show all nodes
        CL.CLAllNode(head);

        //Find node
        System.out.println("Please enter the keyword to find the node");
        key = sc.next();
        node = CL.CLFindNode(head, key);

        if (node != null) {
            nodeData = node.nodeData;
            System.out.println("Information of node corresponding to keyword" + nodeData.key + ":" + nodeData.name + ":" + nodeData.age);
        } else {
            System.out.println("The information of the node corresponding to the node was not found");
        }
        //Delete node information
        System.out.println("Please enter the keyword of the node to be deleted");
        key = sc.next();
        CL.CLDeleteNode(head,key);

        //Display all node information
        CL.CLAllNode(head);
    }
}


insert

lookup

delete

Topics: Java Algorithm data structure linked list