Data structure - singly linked list

Article directory

Table of contents

Article directory

1. What is a linked list?

Linear table:

Sequence table:

2. Classification and implementation of linked lists

Classification:

accomplish:

1.Create node class

2. Create a singly linked list 

1.addTail (tail increase)

 

2. Delete the first node whose node value is key

 3. Insert node (at specified position)

4. Get the length of the linked list 

Summarize


Preface

Hello everyone, this blog will tell you what a linked list is and the implementation process of a singly linked list.


1. What is a linked list?

When explaining what a linked list is, let me first explain to you the general classification linear list and sequence list.

Linear table:

A linear list is a finite sequence of n data elements with the same properties. Linear table is a data structure widely used in practice. Common linear tables: linked list, stack, queue... Linear table is logically a linear structure, that is to say, it is a continuous straight line. However, the physical structure is not necessarily continuous. When the linear table is physically stored, it is usually stored in the form of an array and a chain structure.

 

It can be seen that the linked list is not continuous in memory, but logically continuous, and the nodes in the linked list are stored in memory like stitches 

Sequence table:

The sequence table is a linear structure in which data elements are sequentially stored in a storage unit with continuous physical addresses, and is generally stored in an array. Complete the addition, deletion, checking and modification of data on the array.


2. Classification and implementation of linked lists

Classification:

The linked list can be divided into two types according to whether it contains a head node, and it can also be divided into two types according to whether it is circular

Among them, the difference between loop and non-loop is relatively small, and we will discuss whether to include the head node below.

Question 1: What is the difference between a linked list with a head node and a linked list without a head node?

The first node of the linked list with the leading node does not store data, so it is relatively easy to implement

The first node without the leading node is the data node, which is more difficult to implement than the leading node, and it is the focus of brushing questions and interviews

Below I will take you to implement the most classic singly linked list: an acyclic linked list without a head node.

accomplish:

Idea analysis:

1. Create the node class Node to represent the nodes in the linked list

2. Create a singly linked list SingleList

3. Implement methods to add, delete, check...

1.Create node class

Let's think about the information that should exist in the node class?

The information recorded in the nodes in daily work and life is varied. For the sake of simplicity, we use the simplest one, that is, the nodes only contain

val and the pointer to the next node next

class Node{
    public int val;
    public Node next;

    public Node(int val){
        this.val = val;
    }
}

2. Create a singly linked list 

With the node class, we can combine nodes one by one into a logically continuous linked list.

So what should exist in a singly linked list?

First of all, there should be a head node. Otherwise, we cannot know where the linked list starts, and we cannot pass the linked list as a parameter.

Secondly, there should be a construction method, otherwise it would be meaningless to write such a linked list

There are also some methods, the basic code is given, let's implement them one by one

public class SingleListDemo {
    private Node head;

    public SingleListDemo(){
    }

}


class Node{
    public int val;
    public Node next;

    public Node(int val){
        this.val = val;
    }
}

1.addTail (tail increase)

 

public void addTail(int val) {
    Node newNode = new Node(val);

    if (head == null) {
        head = newNode;
        return;
    }

    Node cur = head;
    while (cur.next != null) {
        cur = cur.next;
    }

    cur.next = newNode;
}

Here we can extend the situation of inserting nodes at the head, that is, point the next pointer of the new node to the head node, and change the pointing of the head node to a new node 

2. Delete the first node whose node value is key

// Delete the first node whose Data value is key 
public void DeleteKey(int key) { 
    // Determine whether the linked list is empty 
    if (head == null) { 
        System.out.println("The linked list is empty"); 
        return; 
    } 
    // Determine whether the first node is the node to be deleted 
    if (head.val == key) { 
        HeadDelete(); 
        return; 
    } 
    Node cur = head; 
    while (cur.next != null) { 
        if ( cur.next.val == key) { 
            cur.next = cur.next.next; 
            return; 
        } 
        cur = cur.next; 
    } 
}

 Extension: Delete all nodes whose value is key, the time complexity is required not to exceed O(n)

// Method 1 loops through 
public void Delete(int Data) { 
    if (head == null) { 
        return; 
    } 
    while (head.val == Data) { 
        head = head.next; 
        if (head == null) { 
            return ; 
        } 
    } 
    Node cur = head;//Define a temporary node to replace the head node to traverse the linked list and delete elements 
    while (true) { 
        if (cur.next == null) { 
            return; 
        } 

        if (cur.next.val == Data ) { 
            System.out.println("Delete successfully!"); 
            cur.next = cur.next.next; 
            continue; 
        } 
        cur = cur.next; 
    } 

} 

// Method 2 - Double pointer
public void Delete2(int Data) {
    if (head == null) {
        return;
    }
    Node pre = head;
    Node cur = head.next;

    while (cur != null) {
        if (cur.val == Data) {
            pre.next = cur.next;
        } else {
            pre = cur;
        }
        cur = cur.next;
    }
    if (head.val == Data) {
        head = head.next;
    }
}

 3. Insert node (at specified position)

// Insert a node at the specified position, the first node is subscript 0 
public void InsertAny(int index, int val) { 
    // Determine whether the linked list subscript is legal 
    if (index < 0 && index > getSize()) { 
        System .out.println("Invalid subscript!"); 
        return; 
    } 
    if (index == 0) { 
        HeadAdd(val); 
        return; 
    } 
    Node node = new Node(val); 
    Node cur = head; 
    // Traverse The linked list finds the element with index-1 
    while (index > 1) {//index-1 
        index--; 
        cur = cur.next; 
    } 
    node.next = cur.next; 
    cur.next = node; 
}

4. Get the length of the linked list 

public int getSize() {
    Node node = head;
    int size = 0;
    while (node != null) {
        node = node.next;
        size++;
    }
    return size;
} 

The above are some of the more important methods. See you in the next issue. 


Summarize

Please practice more questions, the linked list is the focus of the written test questions in the interview!

Guess you like

Origin blog.csdn.net/weixin_73869209/article/details/132701241