Linked list of Redis data structure

One: the realization of linked list and linked list nodes

1: Each linked list node uses an adlist.h/listNode structure to represent:

typedef struct listNode {
    //前置节点
    struct listNode *prev;
    //后置节点
    struct listNode *next;
    //节点的值
    void *value;
} listNode;

Multiple listNodes can form a double-ended linked list through prev and next pointers, as shown in the figure below.

Although multiple listNode structures can form a linked list, it is more convenient to operate when using adlist.h/list to hold the linked list.

typedef struct list {
    //表头节点
    listNode *head;
    //表尾节点
    listNode *tail;
    //节点值复制函数
    void *(*dup)(void *ptr);
    //节点值释放函数
    void (*free)(void *ptr);
    //节点值对比函数
    int (*match)(void *ptr, void *key);
    //链表所包含的节点数量
    unsigned long len;
} list;
  • The dup function is used to copy the value saved by the node of the linked list.
  • The free function is used to release the value saved by the linked list node.
  • The match function is used to compare whether the value saved by the node is equal to the value of another input.

The following figure is a linked list composed of a list structure and three listNode structures.

Two: the characteristics of Redis linked list implementation

  • Double-ended: The linked list node has prev and next pointers, and the complexity of obtaining the pre-node and post-node of a node is O(1).
  • Acyclic: The prev pointer of the head node and the next pointer of the tail point to NULL, and the access to the linked list ends with NULL.
  • With head pointer and tail pointer: through the head pointer and tail pointer of the list structure, the complexity of the program to obtain the head node and the tail node of the linked list is o(1).
  • Linked list length counter: The program obtains the len attribute value of the list structure to obtain the number of nodes, so the complexity of obtaining the number of nodes in the linked list is 0 (1).
  • Polymorphism: The linked list node uses the void* pointer to store the node value, and the type-specific function can be set for the node value through the dup, free, and match three attributes of the list structure, so the linked list can be used to store various types of values.

Three: API of linked list and linked list node

Write picture description here

Write picture description here

Write picture description here

  • The listCreate function is implemented as follows:
/* Create a new list. The created list can be freed with
 * AlFreeList(), but private value of every node need to be freed
 * by the user before to call AlFreeList().
 *
 * On error, NULL is returned. Otherwise the pointer to the new list. */
list *listCreate(void)
{
    struct list *list;

    if ((list = zmalloc(sizeof(*list))) == NULL)
        return NULL;
    list->head = list->tail = NULL;
    list->len = 0;
    list->dup = NULL;
    list->free = NULL;
    list->match = NULL;
    return list;
}
  • The listSetDupMethod function is implemented as follows:
#define listSetDupMethod(l,m) ((l)->dup = (m))
  • The listFirst function is implemented as follows:
#define listFirst(l) ((l)->head)

 

 

Guess you like

Origin blog.csdn.net/qq_37469055/article/details/114419700