当前位置:网站首页>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;
}
边栏推荐
- Reasons for "unresolved external symbols" during vs or QT compilation link:
- 代码规范 & 详细解释 husky、prettier、eslint、lint-staged 的作用和使用
- [software project management] sorting out knowledge points for final review
- OpenCV图像处理-灰度处理
- MySQL performance monitoring and SQL statements
- SQL Server 基础介绍整理
- Pit record_ TreeSet custom sorting results in less data loss
- Installer MySQL sous Linux [détails]
- Oracle sqlplus query result display optimization
- MySQL 10th job - View
猜你喜欢

Origin of b+ tree index

Idea remote debugger

Concise course of probability theory and statistics in engineering mathematics second edition review outline

Docker中实现MySQL主从复制

栖霞市住建局和消防救援大队开展消防安全培训

QT connection MySQL data query failed

MySQL 12th job - Application of stored procedure

MySQL模糊查询详解

滑动窗口

Jasperreports - print PDF (project tool)
随机推荐
ISO 26262之——2功能安全概念
Developers, what is the microservice architecture?
OpenCV图像处理-灰度处理
Unity使用SteamVRPlugin时如何不让其他Camera位置和旋转收到SteamVRPlugin控制
Easyx-----c语言实现2048
2021 Q3-Q4 Kotlin Multiplatform 使用现状 | 调查报告
用同花顺手机炒股是安全的吗?如何用同花顺炒股
Concise course of probability theory and statistics in engineering mathematics second edition review outline
Bit operation n & (n-1), leetcode231, interview question 05.06
Common interview questions of binary tree
Grain Mall - High Availability Cluster
Fabric.js 上划线、中划线(删除线)、下划线
nacos2.x.x启动报错信息Error creating bean with name ‘grpcClusterServer‘;
基础-MySQL
【在线仿真】Arduino UNO PWM 控制直流电机转速
AIX基本操作记录
代码规范 & 详细解释 husky、prettier、eslint、lint-staged 的作用和使用
磁带库简单记录1
wangEditor 上传本地视频修改
Is it safe to use flush mobile phones to speculate in stocks? How to fry stocks with flush