Linked list (concept, implementation - display by adding elements)

Linked List: linked List is one of the implementation classes of List

Storage structure in memory:

How to store data: chain storage - store data in a node way (the storage way is unordered, but the data is still orderly)

Each node contains two contents: 1-date domain (to store the data), 2-next domain (to store the next node)

The difference of linked list: the linked list of lead node & the linked list of no lead node

--Characteristics of linked list data storage: each node of linked list is not necessarily a continuous and orderly storage

Logical structure: from the 'data domain store data' and 'next domain store next linked list pointer' contained in the linked list, we know that the linked list is to find the next value through the address (the logical structure shows that it is continuous, and the address is connected with the address). In fact, the link in the memory is jumping;

Logic level:

Actual storage structure in memory:

Linked list Example of application scenario value: the client sends the user's friends to the server out of order, and the ID of the friends is out of order; the server needs to realize the orderly return of the friends sent from the client to the client (the time and space efficiency is required to be high, and the OutByte cannot be stored in it -- so it can be realized through the orderly storage of the linked list before it is stored in the server )

Test code:

package Linked_List;

import javax.swing.*;

public class LinkedListDemo {
    public static void main(String[] args) {
        HeroNode hero1 = new HeroNode(1,"Songjiang","Timely rain");
        HeroNode hero2 = new HeroNode(2,"Lu Junyi","Jade Unicorn");
        HeroNode hero3 = new HeroNode(3,"Wu Yong","Intelligent multi star");
        HeroNode hero4 = new HeroNode(4,"Linchong","Leopard head");

        SingleLinkedList singleLinkedList = new SingleLinkedList();
        //join
        singleLinkedList.add(hero1);
        singleLinkedList.add(hero2);
        singleLinkedList.add(hero3);
        singleLinkedList.add(hero4);

        singleLinkedList.list();    //display
    }
}

/*
1,Create a method to add nodes first (add information about different nodes)
2,Create head node
3,Create an add method in the head node
4,Adding elements to the main method and traversing the print
 */

//Create SingleLinkedList management hero
class SingleLinkedList {

    //Initialize the header node without placing any specific information
    private HeroNode head = new HeroNode(0, "", "");

    //Add node to one-way linked list
    //Idea: when you don't consider adding according to the sequence of numbers
    //1. Find the last node of the current linked list
    //2. Point the next of the last node to the new node
    public void add(HeroNode hereNode){

        //Because the head node cannot move, a secondary node is needed
        HeroNode temp = head;
        //Traverse the loop to find the last node
        while(true){
            //Find the end of the list
            if(temp.next == null){
                break;
            }
            temp = temp.next;
        }

        //When you exit the while loop, you find the last node
        //Now point temp to the new node
        temp.next = hereNode;
    }


    //Display link list [traversal]
    public void list() {
        //Judge whether the list is empty
        if (head.next == null) {
            System.out.println("Chain list is empty");
            return;
        }
        //The header node is only used to point to a node, so it can't move. It needs an auxiliary variable to assist in traversal
        HeroNode temp = head.next;
        while(true){

            //Judge whether to the end of the list
            if (temp == null) {
                break;
            }

        //Otherwise, it means that there are nodes in the linked list, and the information of the nodes is output
        System.out.println(temp);
        //Move temp backward, or the loop will die!
        temp = temp.next;
        }
    }
}

//Create hero node
class HeroNode{
    public int no;
    public String Name;
    public String NickName;
    public HeroNode next;

    //constructor 
        public HeroNode(int no,String Name,String Nickname){
            this.no = no;
            this.Name = Name;
            this.NickName = Nickname;
        }

    //Generate toString method
    @Override
    public String toString() {
        return "HereNode{" +
                "no=" + no + ", Name='" + Name + '\'' + ", NickName='" + NickName + '\'' + '}';
    }
}

Posted by Mew151 on Thu, 11 Jun 2020 00:21:31 -0700