Data structure-linked list

Store data in a linear order, but store a pointer to the next node in each node (Pointer). Since it is not necessary to store in order, the insertion and deletion operations of the linked list can reach O(1) complexity. This article will explain the singly linked list and the doubly linked list, in which the doubly linked list will give part of the key code implementation.

1. Singly linked list

A singly linked list (singly linked list) is a kind of linked list. It is composed of nodes. Each node contains a pointer to the next node. The figure below is a singly linked list. The header is empty, and the subsequent nodes of the header are "nodes". 10" (node ​​with data 10), the successor node of "node 10" is "node 20" (node ​​with data 10),...

img

2. Delete the node in the singly linked list

Let's take a look at the operation of deleting a node in a singly linked list. For example, in the following singly linked list, we want to delete "node 30".

Before deletion : The successor node of "Node 20" is "Node 30", and the successor node of "Node 30" is "Node 40".

After deletion : The successor node of "Node 20" is "Node 40".

img

3. Add node to singly linked list

Let's take a look at the operation of adding nodes to a singly linked list. For example, in the following singly linked list, we add "node 15" between "node 10" and "node 20"

Before adding : The successor node of "Node 10" is "Node 20".

After adding : The successor node of "Node 10" is "Node 15", and the successor node of "Node 15" is "Node 20".

img

4. Doubly linked list

A doubly linked list (double-linked list) is a type of linked list. Like a singly linked list, a double-linked list is also composed of nodes. Each of its data nodes has two pointers, which point to the immediate successor and the immediate predecessor respectively. Therefore, starting from any node in the doubly linked list, you can easily access its predecessor and successor nodes. Generally, we construct a two-way circular linked list.

The schematic diagram of the double-linked list is as follows:

img

The header is empty, the successor node of the header is "node 10" (the node with data 10); the successor node of "node 10" is "node 20" (the node with data 10), and the successor of "node 20" The node is "node 10"; the successor node of "node 20" is "node 30", the successor node of "node 30" is "node 20"; ...; the successor node of the last node is the head of the table.

It is not difficult to see that the node definition of a doubly linked list can be represented by the following structure:

//双向链表节点结构
typedef struct dlink_node
{
    
    
    struct dlink_node *prev;
    struct dlink_node *next;
    void *val;  //能存储任意类型数据
}node;

5. Delete the node in the doubly linked list

Let's take a look at the operation of deleting a node in a doubly linked list. For example, in the following singly linked list, we want to delete "node 30".

Before deleting : the successor node of "node 20" is "node 30", and the successor node of "node 30" is "node 20". The successor node of "node 30" is "node 40", and the successor node of "node 40" is "node 30".

After deletion : the successor node of "node 20" is "node 40", and the successor node of "node 40" is "node 20".

img

The key code for deleting a node in a doubly linked list is as follows:

//删除节点pindex
pindex->next->prev = pindex->prev;
pindex->prev->next = pindex->next;
free(pindex); //注意释放节点

6. Add nodes to the doubly linked list

Let's take a look at the operation of adding nodes to a doubly linked list. For example, the following doubly linked list adds "node 15" between "node 10" and "node 20"

Before adding : The successor node of "Node 10" is "Node 20", and the predecessor node of "Node 20" is "Node 10".

After adding : The successor node of "Node 10" is "Node 15", and the predecessor node of "Node 15" is "Node 10". The successor node of "Node 15" is "Node 20", and the successor node of "Node 20" is "Node 15".

img

The key code for adding a node to a doubly linked list is as follows:

//将pnode节点插入到pindex之前
pnode->prev = pindex->prev;
pnode->next = pindex;
pindex->prev->next = pnode;
pindex->prev = pnode;

Guess you like

Origin blog.csdn.net/weixin_44322234/article/details/110137570