Data structure, linked list, single linked list, double linked list, circular linked list, double circular linked list

        Linked list is a basic concept in data structure, which is an implementation of linear list. A linked list is different from a sequential list (an array-based linear list) in that the elements of a linked list do not need to be stored contiguously in memory. Each element of the linked list is represented by a node, and each node has a data part and a pointer part.

The following are the basic types of linked lists:

1. Singly linked list:
    - In a singly linked list, each node contains two parts: one is the data part, and the other is a pointer to the next node.
    - The pointer part of the last node points to `NULL`, indicating the end of the linked list.

The following is a simple C language representation of a singly linked list node:

typedef struct Node {
    int data;           // 数据部分
    struct Node* next;  // 指向下一个节点的指针
} Node;

        The following is the basic operation of the singly linked list. The singly linked list applied for this time is a linked list with empty head (the data field of the head node is empty, and the pointer field points to the next node), and it is also possible to apply for a linked list without empty head.

#include <stdio.h>
#include <stdlib.h>
typedef int data_t;
 //构造链表节点类型
typedef struct node{
    data_t data;//保存数据的数据域
    struct node*next;//保存下一个节点的地址
} linklist ;

//创建链表的头节点
linklist* create_linklist();
//判空
int linklist_is_emtpy(linklist * head);
//求元素个数
int get_num_linklist(linklist* head);
//按位置插入
int insert_by_pos_linklist(linklist* head,int pos,data_t val);
//读数据
int printf_linklist(linklist*head);

int main()
{   
    linklist *head=create_linklist();
    for (int i = 0; i < 5; i++)
    {
        insert_by_pos_linklist(head,i,i+1);
    }
    printf_linklist(head);
    return 0;
}

//读数据
int printf_linklist(linklist*head)
{
    if(head==NULL) return -1;
    if(head->next->data==-1) return -1;
    while (NULL!=head->next)
    {
        printf("%d ",head->next->data);
        head=head->next;
    }
    printf("\n");
    return 0;
}

//创建链表的头节点
linklist* create_linklist()
{
    linklist*head=(linklist*)malloc(sizeof(linklist));
    if(NULL==head) return NULL;
    head->next=NULL;
    head->data=-1;
    return head;
}
//判空
int linklist_is_emtpy(linklist * head)
{
    if(NULL==head) return -1;
    return ((head->next==NULL)?1:0);
}
//求元素个数
int get_num_linklist(linklist* head)
{
    //判head
    if(NULL==head) return -1;
    linklist*p=head->next;//p=head
    int num=0;
    while (p!=NULL)//p->next!=NULL
    {
        num++;
        p=p->next;
    }
    return num;
}
//按位置插入
int insert_by_pos_linklist(linklist* head,int pos,data_t val)
{
    if (head==NULL) return -1;
    //判断位置合法性
    if(pos<0||pos>get_num_linklist(head)) return -1;
    //准备新节点
    linklist* new=(linklist*)malloc(sizeof(linklist));
    new->data=val;
    new->next=NULL;
    //先指向pos-1的位置
    linklist*p=head;
    while (pos--) p=p->next;
    //准备链接
    new->next=p->next;
    p->next=new;
    return 0;
}

2. Double linked list:
    - Each node of the double linked list has two pointers: one points to the next node and the other points to the previous node.
    - This makes it possible to traverse the list in both directions.

3. Circular linked list:
    - A circular linked list is similar to a singly linked list, but its last node does not point to `NULL`, but points back to the first node.
    - This structure forms a loop.

4. Doubly linked list:
    - This is a combination of double linked list and circular linked list. Each node has two pointers, one pointing to the previous node and the other pointing to the next node.
    - The next pointer of the last node points to the first node, and the previous pointer of the first node points to the last node.

Linked lists have the following main advantages:

- Dynamic Size: The size of the linked list can change dynamically at runtime without pre-allocating or defining a maximum length.
- Insertion and deletion operations: Compared with arrays, elements in linked lists can be inserted or deleted more easily without moving other elements.

shortcoming:

- Access speed: The linked list does not allow random access. To access a node, it may be necessary to traverse the linked list from the head node until the desired node is found.
- More memory usage: Linked lists use more memory than arrays since each node requires an extra pointer to store the address of the next node.
- Bad for caching: The non-contiguous memory storage of linked lists may not be as good for CPU caching as arrays.

In some cases, a linked list may be the best choice, when elements are inserted and deleted more frequently than elements are accessed .

Guess you like

Origin blog.csdn.net/qq_52119661/article/details/132356138