源码adlist.c adlist.h,先来看看adlist的结构
/* Node, List, and Iterator are the only data structures used currently. */ typedef struct listNode { struct listNode *prev; struct listNode *next; void *value; } listNode; //标准的双向链表 typedef struct listIter { listNode *next; int direction; } listIter; //迭代器 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; //链表本身的结构,一头一尾,三个函数指针,最后一个记录链表长度 结构很简单哦
来看看方法
/* 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; } /* Free the whole list. * * This function can't fail. */ void listRelease(list *list) { unsigned long len; listNode *current, *next; current = list->head; len = list->len; while(len--) { next = current->next; if (list->free) list->free(current->value); zfree(current); current = next; } zfree(list); } //一个创建,一个释放,很干净的代码 //释放时取链表的头部指针,while循环,头部依次next,一个一个free
/* Add a new node to the list, to head, contaning the specified 'value' * pointer as value. * * On error, NULL is returned and no operation is performed (i.e. the * list remains unaltered). * On success the 'list' pointer you pass to the function is returned. */ list *listAddNodeHead(list *list, void *value) { listNode *node; if ((node = zmalloc(sizeof(*node))) == NULL) return NULL; node->value = value; if (list->len == 0) {//如果链表长度还是0,则说明这加入的节点是唯一的节点,则头尾都是这个节点 list->head = list->tail = node; node->prev = node->next = NULL; } else { //这是插入头部的,所以新插入的节点为新的head node->prev = NULL; node->next = list->head; list->head->prev = node; list->head = node; } list->len++;//记得把链表长度+1 return list; }
/* Add a new node to the list, to tail, contaning the specified 'value' * pointer as value. * * On error, NULL is returned and no operation is performed (i.e. the * list remains unaltered). * On success the 'list' pointer you pass to the function is returned. */ list *listAddNodeTail(list *list, void *value) { listNode *node; if ((node = zmalloc(sizeof(*node))) == NULL) return NULL; node->value = value; if (list->len == 0) { list->head = list->tail = node; node->prev = node->next = NULL; } else { node->prev = list->tail; node->next = NULL; list->tail->next = node; list->tail = node; } list->len++; return list; } //插入尾部节点
list *listInsertNode(list *list, listNode *old_node, void *value, int after) { listNode *node; if ((node = zmalloc(sizeof(*node))) == NULL) return NULL; node->value = value; if (after) { //这个after意思是插入到old_node的后面(这里我来打个比方,1,2,3,5,6,把4插入到3的后面,4的前节点赋值为3,4的后节点赋值为3的现在的后节点5) node->prev = old_node; node->next = old_node->next; if (list->tail == old_node) { list->tail = node; } } else { //4的后节点赋值为3,4的前节点赋值为3的现在的前节点2 node->next = old_node; node->prev = old_node->prev; if (list->head == old_node) { list->head = node; } } if (node->prev != NULL) { //先看after,4的前节点3的后节点赋值为4,不赋值前3的后节点是5 //再看非after,4的前节点2的后节点赋值为4,不赋值前2的后节点是3 node->prev->next = node; } if (node->next != NULL) { //先看after,4的后节点5的前节点赋值为4,不赋值前5的前节点是3,这样最后就变成了1,2,3,4,5,6 //再看非after,4的后节点3的前节点赋值为4,不赋值前3的后节点是5,这样最后就变成了1,2,4,3,5,6 node->next->prev = node; } list->len++;//记得长度+1 return list; } //如果不清楚,动手划划
/* Remove the specified node from the specified list. * It's up to the caller to free the private value of the node. * * This function can't fail. */ //把中间的删除,就是让删除的前节点的next指向删除的next,删除的后节点的prev指向删除的prev,再注意一下头尾特殊情况,长度-1 void listDelNode(list *list, listNode *node) { if (node->prev) node->prev->next = node->next; else list->head = node->next; if (node->next) node->next->prev = node->prev; else list->tail = node->prev; if (list->free) list->free(node->value); zfree(node); list->len--; }
/* Returns a list iterator 'iter'. After the initialization every * call to listNext() will return the next element of the list. * * This function can't fail. */ listIter *listGetIterator(list *list, int direction) { listIter *iter; if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL; if (direction == AL_START_HEAD) iter->next = list->head; else iter->next = list->tail; iter->direction = direction; return iter; } /* Release the iterator memory */ void listReleaseIterator(listIter *iter) { zfree(iter); } /* Create an iterator in the list private iterator structure */ void listRewind(list *list, listIter *li) { li->next = list->head; li->direction = AL_START_HEAD; } void listRewindTail(list *list, listIter *li) { li->next = list->tail; li->direction = AL_START_TAIL; } //迭代器的创建与释放,迭代器是单向的,不是从head开始就是tail开始
/* Return the next element of an iterator. * It's valid to remove the currently returned element using * listDelNode(), but not to remove other elements. * * The function returns a pointer to the next element of the list, * or NULL if there are no more elements, so the classical usage patter * is: * * iter = listGetIterator(list,<direction>); * while ((node = listNext(iter)) != NULL) { * doSomethingWith(listNodeValue(node)); * } * * */ listNode *listNext(listIter *iter) { listNode *current = iter->next; if (current != NULL) { if (iter->direction == AL_START_HEAD)//判断迭代器的方向,将下一个赋值给iter的next iter->next = current->next; else iter->next = current->prev; } return current; }
/* Duplicate the whole list. On out of memory NULL is returned. * On success a copy of the original list is returned. * * The 'Dup' method set with listSetDupMethod() function is used * to copy the node value. Otherwise the same pointer value of * the original node is used as value of the copied node. * * The original list both on success or error is never modified. */ list *listDup(list *orig) { list *copy; listIter *iter; listNode *node; if ((copy = listCreate()) == NULL) return NULL; copy->dup = orig->dup; copy->free = orig->free; copy->match = orig->match; iter = listGetIterator(orig, AL_START_HEAD); while((node = listNext(iter)) != NULL) { void *value; if (copy->dup) { value = copy->dup(node->value); if (value == NULL) { listRelease(copy); listReleaseIterator(iter); return NULL; } } else value = node->value; if (listAddNodeTail(copy, value) == NULL) { listRelease(copy); listReleaseIterator(iter); return NULL; } } listReleaseIterator(iter); return copy; } //复制整个list,并且释放old,利用迭代器,一个节点一个节点的复制并释放
/* Search the list for a node matching a given key. * The match is performed using the 'match' method * set with listSetMatchMethod(). If no 'match' method * is set, the 'value' pointer of every node is directly * compared with the 'key' pointer. * * On success the first matching node pointer is returned * (search starts from head). If no matching node exists * NULL is returned. */ listNode *listSearchKey(list *list, void *key) { listIter *iter; listNode *node; iter = listGetIterator(list, AL_START_HEAD); while((node = listNext(iter)) != NULL) { if (list->match) { if (list->match(node->value, key)) { listReleaseIterator(iter); return node; } } else { if (key == node->value) { listReleaseIterator(iter); return node; } } } listReleaseIterator(iter); return NULL; } //查找,迭代器遍历,注意释放迭代器,listReleaseIterator出现了三次
/* Return the element at the specified zero-based index * where 0 is the head, 1 is the element next to head * and so on. Negative integers are used in order to count * from the tail, -1 is the last element, -2 the penultimante * and so on. If the index is out of range NULL is returned. */ listNode *listIndex(list *list, long index) { listNode *n; if (index < 0) { index = (-index)-1; n = list->tail; while(index-- && n) n = n->prev; } else { n = list->head; while(index-- && n) n = n->next; } return n; } //index可以传负值,表示从尾部计算第几个,正值则是从头部计算第几个,0表示头部第一个,-1表示尾部第一个
/* Rotate the list removing the tail node and inserting it to the head. */ void listRotate(list *list) { listNode *tail = list->tail; if (listLength(list) <= 1) return; /* Detatch current tail */ list->tail = tail->prev; list->tail->next = NULL; /* Move it as head */ list->head->prev = tail; tail->prev = NULL; tail->next = list->head; list->head = tail; } //这个方法是将链表的尾部变成链表的头部,即1,2,3,4变成4,1,2,3
总体来看代码比较简单,是个典型的双向链表,但代码清晰,没有多余的逻辑,以后可以做为它用。
相关推荐
解压后,你可以找到包括`redis-server.exe`、`redis-cli.exe`等在内的可执行文件,以及配置文件`redis.conf`。这种方式适合于需要自定义配置或手动管理服务的用户。通过编辑`redis.conf`,你可以调整Redis的各项参数...
本次提供的版本是Redis的稳定版——Redis-x64-5.0.14.1,针对64位操作系统设计。在深入探讨Redis之前,我们先了解下Redis的基本特性。 1. **数据类型**: Redis支持五大数据类型:字符串(String)、哈希(Hash)、列表...
Redis安装包,Redis-x64-3.0.504 windows版安装包
redis-server --service-install redis.windows-service.conf --loglevel verbose 2 常用的redis服务命令。 卸载服务:redis-server --service-uninstall 开启服务:redis-server --service-start 停止服务:redis-...
redis-serviceinstall .cmd安装成服务脚本(redis设置成windows下的服务,redis-serviceinstall .cmd脚本内容如下) redis-server --service-install redis.windows-service.conf --loglevel verbose redis-server -...
redis-stack-server-7.2.0-v9.arm64.snap redis-stack-server-7.2.0-v9.bionic.arm64.tar.gz redis-stack-server-7.2.0-v9.bionic.x86_64.tar.gz redis-stack-server-7.2.0-v9.bullseye.x86_64.tar.gz redis-stack-...
这个名为"Redis-x64-5.0.14.1"的压缩包是Redis针对Windows操作系统的64位版本,版本号为5.0.14.1。在Windows上运行Redis可能与Linux环境有所不同,但仍然提供了相同的核心功能。 1. **Redis的特性**: - **内存...
在Windows环境下,Redis-x64-5.0.14是Redis为64位Windows操作系统编译的一个版本,提供了在Windows上运行Redis的能力。本文将深入探讨Redis的基本概念、功能特性、安装与配置,以及在Windows平台上的使用方法。 ...
之前项目中要使用window下的...找了好久都只有Redis-x64-3.2.100的,后来发现有更高版本的。这里附件里面包括Redis-x64-5.0.14.1 for Windows版本 与及 原始下载地址。如果有新的版本的话,可以到在原始下载地址找到。
Redis-x64-3.0.504安装包是一个针对64位操作系统的Redis数据库服务器的安装程序。Redis是一款高性能、开源、基于键值对的数据存储系统,广泛应用于缓存、数据库、消息中间件等多个场景。这个版本是3.0.504,代表着在...
**Redis 全面检查工具:redis-full-check** Redis 是一款高性能的键值存储系统,广泛应用于缓存、数据库和消息中间件等场景。在实际应用中,为了确保 Redis 的稳定性和数据一致性,需要定期对 Redis 实例进行健康...
这个压缩包“Redis-x64-5.0.14.1.msi”显然是 Redis 的 Windows 64 位版本的安装程序,版本号为 5.0.14。下面将详细介绍 Redis 的核心概念、功能以及使用方法。 1. **Redis 简介**:Redis 是 Remote Dictionary ...
这个“Redis-x64-5.0.9.zip”压缩包包含的是适用于64位操作系统的Redis服务器的5.0.9版本。在本文中,我们将深入探讨Redis的主要特性和功能,以及如何安装和使用这个版本。 1. **Redis的数据类型**: - 字符串...
本篇文章将详细讲解基于标题"Windows版本Redis-x64-5.0.14安装包"的Redis安装过程,以及如何在Windows上配置和使用Redis。 首先,你需要下载Redis的Windows版本,这里提到的是Redis-x64-5.0.14。这个版本适用于64位...
赠送jar包:flink-connector-redis_2.10-1.1.5.jar; 赠送原API文档:flink-connector-redis_2.10-1.1.5-javadoc.jar; 赠送源代码:flink-connector-redis_2.10-1.1.5-sources.jar; 赠送Maven依赖信息文件:flink-...
Redis-x64-3.0.504同时启动6个redis服务Windows环境,以及修改好端口,直接打开快捷方式即可同时打开服务,6个redis服务,Redis-x64-3.0.504端口:6379 6380 6381 6832 6383 6384 有些场景中,我们需要一台服务器...