当前位置:网站首页>02 linked list of redis data structure
02 linked list of redis data structure
2022-06-26 11:01:00 【Pinellia ternata (• ̤̀ ᵕ• ̤́ ๑)ᵒᵏᵎᵎᵎᵎ】
Linked list
Introduction of the list
Specific introduction and reference of linked list : Several forms of linked lists, Joseph rings and LRU Design of cache elimination algorithm (java、C、Go Language implementation ), stay Java In language , We have LinkedList It is based on linked list , But in C There is no such built-in data structure in the language , therefore redis Built a Double linked list , Mainly used in List The underlying implementation of this data structure , In publishing and subscribing 、 The slow query 、 Monitor and other functions are also useful .

Here is redis Definition of bidirectional linked list in
// adlist.h
#ifndef __ADLIST_H__
#define __ADLIST_H__
/* * Double ended list node */
typedef struct listNode {
// Precursor node
struct listNode* prev;
// The subsequent nodes
struct listNode* next;
// The value of the node
void* value;
} listNode;
/* * Double ended linked list iterator */
typedef struct listIter {
// Current iteration node
listNode* next;
// Iteration direction
int direction;
} listIter;
typedef struct list {
listNode* head;
listNode* tail;
unsigned long len;
} list;
// Iterate from header to tail
#define AL_START_HEAD 0
// Iterate from the end of the table to the head
#define AL_START_TAIL 1
#endif
Core code
Create a linked list
// adlist.c
/* * Create a new list * * Successful creation returns the linked list , Failure to return NULL . * * T = O(1) */
list* listCreate() {
struct list* list;
list = malloc(sizeof(struct list));
if (list == NULL) return NULL;
list->head = list->tail = NULL;
list->len = 0;
list->dup = NULL;
list->free = NULL;
list->match = NULL;
return list;
}
Insert nodes in the list
// adlist.c
/* * Insert a node into the chain header * * T = O(1) */
list* listAddNodeHead(list* list, void* value) {
listNode* node;
node = malloc(sizeof(struct listNode));
if (node == NULL) return NULL;
node->value = value;
if (list->len == 0) {
list->head = node;
list->tail = node;
node->prev = NULL;
node->next = NULL;
} else {
node->prev = NULL;
node->next = list->head;
list->head->prev = node;
list->head = node;
}
list->len++;
return list;
}
/* * Insert a node at the end of the linked list * * T = O(1) */
list* listAddNodeTail(list* list, void* value) {
listNode* node;
node = malloc(sizeof(struct listNode));
if (node == NULL) return NULL;
node->value = value;
if (list->len == 0) {
list->head = node;
list->tail = node;
node->prev = NULL;
node->next = NULL;
} else {
node->prev = list->tail;
node->next = NULL;
list->tail->next = node;
list->tail = node;
}
list->len++;
return list;
}
/* * Insert into old_node Before or after * * If after by 0 , Insert the new node into old_node Before . * If after by 1 , Insert the new node into old_node after . * * T = O(1) */
list* listInsertNode(list *list, listNode *old_node, void *value, int after) {
listNode* node;
node = malloc(sizeof(struct listNode));
if (node == NULL) return NULL;
node->value = value;
if (after) {
node->prev = old_node;
node->next = old_node->next;
old_node->next = node;
if (list->tail == old_node) {
list->tail = node;
}
} else {
node->next = old_node;
node->prev = old_node->prev;
if (list->head == old_node) {
list->head = node;
}
}
// take old_node Of prev Point to node
if (node->prev != NULL) {
node->prev->next = node;
}
// take old_node Of next Point to node
if (node->next !=NULL) {
node->next->prev = node;
}
list->len++;
return list;
}
Delete node from linked list
/* * From the list list Delete the given node in node * * Private value to node (private value of the node) The release of is performed by the caller . * * T = O(1) */
void listDelNode(list *list, listNode *node) {
if (node->prev != NULL) {
node->prev->next = node->next;
} else {
list->head = node->next;
}
if (node->next != NULL) {
node->next->prev = node->prev;
} else {
list->tail = node->prev;
}
free(node);
list->len--;
}
Iteration of linked list
/* * Create an iterator for a given linked list * * T = O(1) */
listIter *listGetIterator(list *list, int direction) {
listIter* iter;
iter = malloc(sizeof(struct listIter));
if (iter == NULL) return NULL;
if (direction == AL_START_HEAD) {
iter->next = list->head;
} else {
iter->next = list->tail;
}
iter->direction = direction;
return iter;
}
/* * Set the direction of the iterator to AL_START_HEAD , * And re point the iteration pointer to the header node . * * T = O(1) */
void listRewind(list *list, listIter *li) {
li->next = list->head;
li->direction = AL_START_HEAD;
}
/* * Set the direction of the iterator to AL_START_TAIL , * And re point the iteration pointer to the header node . * * T = O(1) */
void listRewindTail(list *list, listIter *li) {
li->next = list->tail;
li->direction = AL_START_TAIL;
}
/** * Release iterators */
void listReleaseIter(listIter *li) {
free(li);
}
/** * Returns the node to which the iterator is currently pointing . */
listNode* listNext(listIter *iter) {
listNode* current = iter->next;
if (current != NULL) {
if (AL_START_HEAD == iter->direction) {
iter->next = current->next;
} else {
iter->next = current->prev;
}
}
return current;
}
边栏推荐
猜你喜欢

Hazelnut cloud - SMS (tool)
![Installer MySQL sous Linux [détails]](/img/38/77be56c3ef3923ce4c4e5df4a96f41.png)
Installer MySQL sous Linux [détails]

Développeur, quelle est l'architecture des microservices?

MySQL 8th job

携程机票 App KMM 跨端 KV 存储库 MMKV-Kotlin | 开源

Quantitative investment learning - Introduction to classic books

nacos2.x.x启动报错信息Error creating bean with name ‘grpcClusterServer‘;

Which PHP open source works deserve attention

AdaptiveAvgPool2D 不支持 onnx 导出,自定义一个类代替 AdaptiveAvgPool2D

c语言 --- 运算符和表达式
随机推荐
【软件项目管理】期末复习知识点整理
[software project management] sorting out knowledge points for final review
【深度学习理论】(6) 循环神经网络 RNN
Postman入门教程
Sqli-labs靶场1-5
Tape library simple record 1
Using reflection to export entity data to excel
Fabric.js 上划线、中划线(删除线)、下划线
UDP Flood攻击防御原理
Fabric. JS upper dash, middle dash (strikethrough), underline
3、 Linked list exercise
Nuxt. JS - learning notes
Using baijiafan to automatically generate API calls: progress in JS (II)
Oracle11g 启动数据库时报错 ORA-27154: post/wait create failed
Moore vote, leetcode169, leetcode229
Oracle sqlplus query result display optimization
Linux下安裝Mysql【詳細】
Origin of b+ tree index
The sixth MySQL job - query data - multiple conditions
DataBinding使用与原理分析