[Data structure] With both pictures and texts, you can easily grasp the linked list through the logic diagram, and realize various interface functions

insert image description here

Junxi_'s personal homepage

Be diligent and encourage the years to wait for no one

C/C++ game development

Hello, Mina-san, this is Junxi_, we will continue to introduce the content of the primary data structure according to the sequence table we talked about before. Today I will bring you the basic knowledge about the linked list and the realization of various interface functions.
Well, without further ado, let's start today's study!

1. Basic knowledge of linked list

1. The concept and basic structure of linked list

  • Concept: Linked list is a non-sequential and non-sequential storage structure in physical storage structure. The logical order of data elements is realized through the link order of pointers in the linked list.
  • Linked list Linked list, as the name suggests, the structure of the linked list is like being connected, except that the "rope" connecting the linked list in the middle is a pointer.
    insert image description here
  • From the basic structure diagram, we can see:
    1. The chain structure is logically continuous, but unlike the sequence table, the linked list is not necessarily continuous physically.
    2. In reality, the nodes of the linked list are generally from the heap Apply for it.
    3. The space requested from the heap is allocated according to a certain strategy, and the spaces requested twice may be continuous or discontinuous.

2. Classification of linked lists

  • In practice, the structure of the linked list is very diverse, the following is a brief introduction to several
    insert image description here
    insert image description here
    insert image description here

insert image description here

  • Although linked lists have many structures, the most commonly used are these two
    insert image description here
  • 1. Headless one-way non-circular linked list: the structure is simple, and it is generally not used to store data alone. In practice, it is more of a substructure of other data structures, such as hash buckets, adjacency lists of graphs, and so on. In addition, this structure appears a lot in written test interviews.
  • 2. Leading two-way circular linked list: the most complex structure, generally used to store data separately. The linked list data structure used in practice is a two-way circular linked list with the lead. In addition, although this structure is complex, after using the code to implement it, you will find that the structure will bring many advantages, and the implementation will be simpler. I will guide you to implement the code one by one later.

2. Implementation of headless singly linked list

  • Since we are an elementary data structure, and this is the first blog about linked lists, we will start with the simplest headless single linked list.
  • First of all, let's list the interfaces of several functions that we need, and then let's introduce them one by one.

Explanation: The function names and so on of all the following codes are compiled according to the function of the function, that is to say, these function names are not unique, you can also use other names, which will not affect the use of the linked list, but like It’s the same as naming children. We don’t want our children’s names to be Goudan, Ergouzi or something. In fact, in the naming of functions, you can name them like these, so I suggest that whether it is now or in the future It is best to name the functions according to the function, which not only increases the readability of the code, but also allows people to know the function of each function at a glance.

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLTDataType;
typedef struct SListNode
{
    
    
    SLTDataType Data;
    struct SListNode * next;

}SLTNode;
//打印链表
void SLTPrint(SLTNode* phead);
//初始化链表
SLTNode* BuySListNode(SLTDataType x);
void SLTPushBack(SLTNode** pphead, SLTDataType x);
void SLTPushFront(SLTNode** pphead, SLTDataType x);

void SLTPopBack(SLTNode** pphead);
void SLTPopFront(SLTNode** pphead);
// 找某个数
SLTNode* SLTFind(SLTNode* phead, SLTDataType x);

// 在pos之前插入x
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);

// 在pos以后插入x
void SLTInsertAfter(SLTNode* pos, SLTDataType x);

// 删除pos位置
void SLTErase(SLTNode** pphead, SLTNode* pos);

// 删除pos的后一个位置
void SLTEraseAfter(SLTNode* pos);
//修改pos位置的值
void SLTModify(SLTNode**pphead, SLTNode* pos, SLTDataType x);
// 单链表的销毁
void SListDestroy(SLTNode** pphead);
  • Note: The declaration of the interface is included in the header file
  • Let me talk about a few places that need attention here:
  • 1. The first typedef is actually for our convenience, because we don’t know what type of data our linked list is used to store, so we define a SLDataType here, and the data type is uniformly used in the following code It replaces it. In this way, if we want to change the stored data type in the future, we only need to change here. For example, we now want to store double type data
typedef double SLDataType;
  • 2. About the structure of our linked list
typedef struct SListNode
{
    
    
    SLTDataType Data;
    struct SListNode * next;

}SLTNode;
  • 我们说了,我们的链表的连接是通过指针来实现的,因此我们的结构体中第一个成员是该节点存储的值,第二个成员则是一个该结构体的指针,指向的是下一个节点的地址。

1. Initialize the linked list BuySListNode

  • When we want to use our linked list, we first need to initialize it like a variable
//初始化链表
SLTNode* BuySListNode(SLTDataType x)
{
    
    
    SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
    if (newnode == NULL)
    {
    
    
        perror("malloc failed:");
        exit(-1);
    }
    newnode->Data = x;
    newnode->next = NULL;
    return newnode;

}
  • We first opened up space for a node of our linked list through malloc, and then judged whether our malloc was successful through perror. If successful, we set the value of the linked list to the x we ​​input, since there is only one node at this time, So next is empty, so that one of our nodes is initialized.

2. Print linked list SLTPrint

  • When our initialization is successful, we want to print our linked list to see if our linked list is successfully initialized, so we continue to write the function of printing the linked list
//打印链表
void SLTPrint(SLTNode* phead)
{
    
    
    SLTNode* cur = phead;
    while (cur)
    {
    
    
        printf("%d->", cur->Data);
        cur = cur->next;
    }
    printf("NULL\n");
}
  • We define a structure pointer cur to point to the head of the incoming linked list. When cur is not empty, we print the Data in the node at this time and find the next node through next
  • Since the last element of our linked list is NULL, we finally print all the nodes in the linked list, and add a NULL at the end.

insert image description here

  • The effect is as shown above

3. Head plug SLTPushFront and head delete SLTPopFront

  • Head insertion and head deletion, as the name implies, are to insert nodes at the head of the linked list or delete nodes at the head of the linked list

plug

//头插
void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
    
    
    SLTNode* newnode = BuySListNode(x);
    newnode->next = *pphead;
    *pphead = newnode;
}
  • The logic diagram of the head plug is like this
    insert image description here
    insert image description here

head delete

//头删
void SLTPopFront(SLTNode** pphead)
{
    
    
    //空
    assert(*pphead);
    //非空;
    SLTNode* newhead = (*pphead)->next;//保存一下下一个节点的地址
    free(*pphead);
    *pphead = newhead;
}
  • Logic diagram of head deletion
  • First judge whether *pphead is empty, if it is empty, it means that this linked list does not exist at all
    insert image description here
    insert image description here

4. Tail insertion SLTPushBack and tail deletion SLTPopBack

  • In the same way as before, tail insertion and tail deletion are applied to the last of the linked list

tail plug

//尾插
void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
    
    
    SLTNode* newnode = BuySListNode(x);
    SLTNode* tail = *pphead;
    //链表中没有节点时
    if (*pphead == NULL)
    {
    
    
        *pphead = newnode;
    }
    else
    {
    
    
        //不为空时
        while (tail->next)
        {
    
    
            tail = tail->next;
        }
        tail->next = newnode;

    }
}
  • We know that the next address of the last node of the linked list is empty, let's analyze it through the logic diagram
  • First of all, it is a special case. When there is no node in our linked list, then we just point the head pointer directly to the node to be inserted.
    insert image description here
    insert image description here

tail delete

//尾删
void SLTPopBack(SLTNode** pphead)
{
    
    
    //为空
    assert(*pphead);
    //只有一个节点
    if ((*pphead)->next == NULL)
    {
    
    
        free(*pphead);
        *pphead = NULL;
    }
    //不为空且有多个节点
    else
    {
    
    
        SLTNode* tail = *pphead;
        while (tail->next->next)
        {
    
    
            tail = tail->next;
        }
        free(tail->next);
        tail->next = NULL;


    }
}
  • Let's analyze the special situation first, first judge whether the linked list is empty through the assert assertion, and secondly, if the linked list has only one node, we point to free the node and then make it empty, no other operations are required.
  • General case logic diagram
    insert image description here
    insert image description here

Summarize

  • Due to the limited space, today's content is over here. After that, we will finish the remaining interfaces and then take you to do a few oj questions to make everyone more familiar with the use of linked lists. I believe that if you can stick to it all the time, then your basic knowledge of linked lists must be fine! Remember to start typing the code yourself!

  • Well, if you have any questions, please ask me in the comment area or private message, see you next time!

It is not easy for a new blogger to create. If you feel that the content of the article is helpful to you, you may wish to click on this new blogger before leaving. Your support is my motivation to update! ! !

**(Ke Li asks you to support the blogger three times in a row!!! Click the comment below to like and collect to help Ke Li)**

insert image description here

Guess you like

Origin blog.csdn.net/syf666250/article/details/132050062