(Java based) data structure and algorithm analysis -- single linked list

Posted by jabapyth on Mon, 10 Jan 2022 20:21:39 +0100

summary

Baidu Encyclopedia: single linked list is a chain access data structure, which uses a group of storage units with arbitrary address to store the data elements in the linear list. The data in the linked list is represented by nodes. The composition of each node is: element (image of data element) + pointer (indicating the storage location of subsequent elements). The element is the storage unit for storing data, and the pointer is the address data connecting each node.
In short, a linked list is a string of data sets one by one. Data sets are called nodes in the linked list

The linked list in Java is similar to that in C, but we don't need to use pointers here

Node:
The linked list is composed of nodes. The data stored by a node is divided into two parts. The first part is the data we need to store, and the other part is the address (i.e. reference) of the next node. In C language, nodes are structures. In Java, create node classes and replace nodes with objects. Each node must contain an attribute to point to the next node. This attribute type is set as a node class. This attribute is equivalent to the next pointer in the C language structure, that is, to store the address of the next node object.
no is the number and name is the name
Header node:
There is a very important node in the single chain, that is, the head node and the single chain table. The head node is everything. Without the head node, the whole single chain table cannot be traversed. The head node can not store data, only store the address of the next node, or store data.
We use the head node here to store only the address of the next node.

Insert node (create)

Here, create a head node and a node object at the same time, and assign the address of the head node to the new node to prevent cutting directly on the head


Here, in order to test the memory operation, set the formal parameters of the method as an array, and then pass in the array storing the node, traverse the array, and then insert the node.

modify

First access the new node (formal parameter)

delete

Print linked list

test

All codes

public class SingleLinkedListDemo {
    public static void main(String[] args) {
        Node node1=new Node(1,"Songjiang");
        Node node2=new Node(3,"Wu Yong");
        Node node3=new Node(2,"Lu Junyi");
        Node node4=new Node(3,"Wu Yong");
        Node node5=new Node(4,"Wu Yong");
        Node node[]={node1,node2,node3,node4,node5};
        SingleLinkedList linkedList=new SingleLinkedList();
        linkedList.add(node);
        linkedList.list();
        System.out.println();

        linkedList.update(new Node(6,"Big head"));
        linkedList.list();
        System.out.println();

        linkedList.delect(3);
        linkedList.list();
    }
}

//Class for managing linked lists
class SingleLinkedList{
//    Create a header node. The header node is only used as a mark and does not store data
    private Node head=new Node(0,"");
    private Node temp;    //To prevent the head node from changing, create a node here to replace the head node
//    Method of adding nodes
    public void add(Node[] nodeArray){
        temp=head;

//        Separate the operation of traversing the linked list from the operation of inserting nodes

//        Set a flag to judge whether the number is repeated
        boolean flag;
//        Traverse and find the position where the new node should be inserted according to the number from small to large
        for (int i = 0; i < nodeArray.length; i++) {
//            Set the initial value to flag. When the number is repeated, set flag to true
            flag=false;
            while(true){
                if (temp.next==null){
                    break;
                }
                if (temp.next.no>nodeArray[i].no){
                    break;
                }
                if (temp.next.no==nodeArray[i].no){
                    flag=true;
                    break;
                }
                temp=temp.next;
            }
//            Insert operation
            if (flag){
                System.out.println(nodeArray[i].no+"Duplicate number");
                continue;
            }else {
                nodeArray[i].next=temp.next;
                temp.next=nodeArray[i];
            }
        }
    }

//    delete
    public void delect(int no){
        if (head.next==null){
            System.out.println("The linked list is empty");
            return;
        }
//        Traversal search
        temp=head;
        while (true){
            if (temp.next==null){
                System.out.println("not found");
                return;
            }
            //    Temp. Is required for deletion Next, you can't use temp.
            //    If you use temp to find the data to be deleted, you can't find the previous node, and then you can't delete it
            if (temp.next.no==no){
                break;
            }
            temp=temp.next;
        }
//        After finding the node to be deleted, connect the previous node of this node with the next node of this node. If this node is not referenced, it will be garbage collected
        temp.next=temp.next.next;
    }

//    Display all data in the linked list
    public void list(){
        if (head.next==null){
            System.out.println("The linked list is empty");
        }else {
            temp=head.next;   //The head node has no data, so let temp directly point to the second node
            while (temp!=null){
                System.out.println(temp);
                temp=temp.next;
            }
        }

    }

//    Modify node
    public void update(Node node){
//        First, judge whether the linked list is empty
        if (head.next==null){
            System.out.println("The linked list is empty");
            return;
        }
        Node temp=head.next;

//        Here, traverse to find the node with the corresponding number, and traverse to make temp point to the node to be modified
        while (true){
            if (temp==null){
//                If you traverse to the end of the linked list, it means that it is not found. return directly
                System.out.println("not found");
                return;
            }
            if (temp.no==node.no){
                break;
            }
            temp=temp.next;
        }

//        Modify the linked list data after finding it
        temp.name=node.name;
    }
}

//When creating a node class, the properties here can be set as public properties, because this class is only used internally and will not be exposed
class Node{
    public int no;
    public String name;
    public Node next=null;

    public Node(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "node{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}

matters needing attention

You need to know more about temp using head or head Next differences

Topics: Java linked list