[Data structure and algorithm] Single Linked List (SingleLinkedList)



Linked list

1. Introduction to the memory storage structure of the linked list

Insert picture description here

  • The linked list is stored in the form of nodes, which is a chain storage

  • Each node contains data 域: 当前节点的值, next 域:指向下一个节点

  • The nodes of the linked list are not necessarily stored continuously ---- 靠next域进行节点间的连接指向,最后链表的以NULL值结束
    Insert picture description here

  • The linked list is divided into a linked list with a head node and a linked list without a head node, which is determined according to actual needs

Back to top


2. Introduction to the logical structure of the linked list

Insert picture description here

  • The so-called logical structure is actually a chain structure formed by connecting the nodes in the linked list in sequence until the end.

Back to top


3. The application example of singly linked list is realized by using singly linked list with head-Water Margin Heroes Ranking Management

♑ When adding a hero, add it directly to the end of the linked list

Insert picture description here

Single linked list

  • Main 构建含有头节点的单链表features: 实现链表节点的添加, ,链表的输出
  • Use node class to create head node-fixed
  • Adding method of linked list elements: directly add nodes to the end of the linked list.To add a node directly to the end of the linked list, we must first go back to the structure of the linked list, the linked list 最后节点的特点 ---- next域为null, to grasp this point, we traverse the linked list when adding nodes, find the last one ( temp.next = null) of the current linked list , and then add the new The next field assigned by the node to the last node ( temp.next = heroNode)
  • Note that in the whole process we introduce 辅助节点temp, assign the attributes of the head node to it, and let it represent the head node to compare in turn. In a sense, temp can be regarded as a pointer. The key to introducing auxiliary nodes is that the head node is not allowed. changing.
  • Similarly, when outputting the linked list, the auxiliary node is also used for traversal output.
class singleLinkedList{
    
    
    // 创建头节点,固定不动
    private HeroNode head = new HeroNode(0,"","");
    // 添加节点
    public void add(HeroNode heroNode){
    
    
        // 借助辅助节点进行遍历
        HeroNode temp = head;
        // 循环遍历,直到找不到下一个(以达到当前链表的最后一个节点)
        while (true){
    
    
            if (temp.next == null){
    
    
                break;
            }
            // 不是最后一个,后移一个节点
            temp = temp.next;
        }
        // 当退出循环的时候,temp指向了链表的最后
        // 将最后节点的next指向新的节点
        temp.next = heroNode;
    }
    // 显示链表
    public void showList(){
    
    
        // 既然显示链表,那就得遍历,首先判断链表是否为空
        // 如果头节点的下一个为空,那么该链表为空
        if (head.next == null){
    
    
            System.out.println("该链表为空!");
            return;
        }
        // 同样借助辅助节点
        HeroNode temp = head.next;
        // 循环遍历
        while (true){
    
    
            // 如果temp为空,表示到了链表的最后
            if (temp == null){
    
    
                break;
            }
            // 输出信息
            System.out.println(temp);
            // 节点后移
            temp = temp.next;
        }
    }
}

Node class

  • The main function:创建节点
  • Declare the structure of the node and the information it contains
// 创建节点,每个节点就是一个对象
class HeroNode{
    
    
    public int no;          // 编号
    public String name;
    public String nickName;
    public HeroNode next;   // 下一个节点

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

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

test

package 链表;

public class singleLinkedLsitDemo {
    
    
    public static void main(String[] args) {
    
    
        // 创建一个单链表
        singleLinkedList singleLinkedList = new singleLinkedList();
        // 创建多个节点
        HeroNode hero1 = new HeroNode(1,"宋江","及时雨");
        HeroNode hero2 = new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode hero3 = new HeroNode(3,"吴用","智多星");
        HeroNode hero4 = new HeroNode(4,"林冲","豹子头");
        HeroNode hero5 = new HeroNode(5,"李逵","黑旋风");
        // 向链表中添加节点
        singleLinkedList.add(hero1);
        singleLinkedList.add(hero2);
        singleLinkedList.add(hero3);
        singleLinkedList.add(hero4);
        singleLinkedList.add(hero5);
        // 输出链表
        singleLinkedList.showList();
    }
}

Insert picture description here

Back to top


♑ When adding a hero, insert the hero into the specified position according to the ranking

Insert picture description here
Realize the method of adding according to the ranking

  • The main function:找到排名位置进行节点插入
  • Here there are two main requirements: 1.对链表的添加元素实行有序添加;2.对于已存在编号的元素舍弃添加
  • On the basis of the add() method directly added to the end of the linked list, the difficulty lies in finding the location where the new node is added. The so-called order is based on the number information of the node ( no), then we compare the numbers and still use the auxiliary node for traversal.
  • When traversal of 辅助节点.nextand 新节点numbers for size comparison (see FIG binding will be more readily understood), after obtaining the position, the next field will be directed in series list.
// 进行排名添加
public void addByOrder(HeroNode heroNode) {
    
    
    // 通过编号顺序添加到列表中,若已存在编号,则不能添加
    HeroNode temp = head;
    boolean flag = false; // 标志是否已存在某一编号的节点
    while (true) {
    
    
        // 链表为空
        if (temp.next == null) {
    
    
            break;
        }
        // 在插入的时候,是位于添加位置的前一个节点
        if (temp.next.no > heroNode.no) {
    
    
            break; // 添加位置找到
        } else if (temp.next.no == heroNode.no) {
    
    
            flag = true; // 编号已存在
            break;
        }
        // 节点后移
        temp = temp.next;
    }
    // 判断状态
    if (flag) {
    
    
        System.out.printf("该英雄的编号 %d 已经存在\n", heroNode.no);
    } else {
    
    
        // 将新的节点加入到链表中
        heroNode.next = temp.next;
        temp.next = heroNode;
    }
}

test

public class singleLinkedLsitDemo {
    
    
    public static void main(String[] args) {
    
    
        // 创建一个单链表
        singleLinkedList singleLinkedList = new singleLinkedList();
        // 创建多个节点
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
        HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");
        HeroNode hero5 = new HeroNode(5, "李逵", "黑旋风");

        // 向链表中添加节点(乱序)
        singleLinkedList.addByOrder(hero2);
        singleLinkedList.addByOrder(hero5);
        singleLinkedList.addByOrder(hero1);
        singleLinkedList.addByOrder(hero4);
        singleLinkedList.addByOrder(hero3);

        // 输出链表
        singleLinkedList.showList();
    }
}

Insert picture description here

Back to top


Guess you like

Origin blog.csdn.net/qq_45797116/article/details/113446911