新增adlisr.h adlist.c部分注释
This commit is contained in:
parent
28ae5daf6c
commit
0c7fff8fdf
@ -39,6 +39,7 @@
|
||||
* listSetFreeMethod.
|
||||
*
|
||||
* On error, NULL is returned. Otherwise the pointer to the new list. */
|
||||
//链表创建
|
||||
list *listCreate(void)
|
||||
{
|
||||
struct list *list;
|
||||
@ -54,6 +55,7 @@ list *listCreate(void)
|
||||
}
|
||||
|
||||
/* Remove all the elements from the list without destroying the list itself. */
|
||||
//清空列表所有元素
|
||||
void listEmpty(list *list)
|
||||
{
|
||||
unsigned long len;
|
||||
@ -74,6 +76,7 @@ void listEmpty(list *list)
|
||||
/* Free the whole list.
|
||||
*
|
||||
* This function can't fail. */
|
||||
//释放整个列表
|
||||
void listRelease(list *list)
|
||||
{
|
||||
listEmpty(list);
|
||||
@ -86,6 +89,7 @@ void listRelease(list *list)
|
||||
* 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;
|
||||
@ -112,6 +116,7 @@ list *listAddNodeHead(list *list, void *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;
|
||||
@ -123,9 +128,13 @@ list *listAddNodeTail(list *list, void *value)
|
||||
list->head = list->tail = node;
|
||||
node->prev = node->next = NULL;
|
||||
} else {
|
||||
//当前要插入的节点的头指向链表的尾指针
|
||||
node->prev = list->tail;
|
||||
//当前要插入节点的下一个节点置为NULL
|
||||
node->next = NULL;
|
||||
//链表的尾节点的下一个节点指向当前要插入的节点
|
||||
list->tail->next = node;
|
||||
//链表的尾指针指向当前要插入的节点
|
||||
list->tail = node;
|
||||
}
|
||||
list->len++;
|
||||
|
54
src/adlist.h
54
src/adlist.h
@ -27,51 +27,67 @@
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* 双向链表 被广泛运用于 redisClients、redisServer、发布订阅、慢查询、监视器等。
|
||||
* 有点:
|
||||
* 在插入、删除结点的时间复杂度很低
|
||||
* 缺点
|
||||
* 内存利用率低
|
||||
* 由于内存不连续,容易产生内存碎片
|
||||
*/
|
||||
#ifndef __ADLIST_H__
|
||||
#define __ADLIST_H__
|
||||
|
||||
/* Node, List, and Iterator are the only data structures used currently. */
|
||||
|
||||
// 链表结点
|
||||
typedef struct listNode {
|
||||
struct listNode *prev;
|
||||
struct listNode *next;
|
||||
// 可以指向任何类型的值
|
||||
void *value;
|
||||
} listNode;
|
||||
|
||||
// 迭代器,负责所有和链表遍历有关的行为
|
||||
// 遍历 list 时,只需要调用 listIter 的遍历函数即可
|
||||
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;
|
||||
|
||||
/* Functions implemented as macros */
|
||||
#define listLength(l) ((l)->len)
|
||||
#define listFirst(l) ((l)->head)
|
||||
#define listLast(l) ((l)->tail)
|
||||
#define listPrevNode(n) ((n)->prev)
|
||||
#define listNextNode(n) ((n)->next)
|
||||
#define listNodeValue(n) ((n)->value)
|
||||
#define listLength(l) ((l)->len) //链表长度
|
||||
#define listFirst(l) ((l)->head) //链表头
|
||||
#define listLast(l) ((l)->tail) //链表尾
|
||||
#define listPrevNode(n) ((n)->prev) //上一个节点
|
||||
#define listNextNode(n) ((n)->next) // 下一个几点
|
||||
#define listNodeValue(n) ((n)->value) //节点值
|
||||
|
||||
#define listSetDupMethod(l,m) ((l)->dup = (m))
|
||||
#define listSetFreeMethod(l,m) ((l)->free = (m))
|
||||
#define listSetMatchMethod(l,m) ((l)->match = (m))
|
||||
#define listSetDupMethod(l,m) ((l)->dup = (m)) // 节点复制
|
||||
#define listSetFreeMethod(l,m) ((l)->free = (m)) // 节点释放
|
||||
#define listSetMatchMethod(l,m) ((l)->match = (m)) //节点对比
|
||||
|
||||
#define listGetDupMethod(l) ((l)->dup)
|
||||
#define listGetFreeMethod(l) ((l)->free)
|
||||
#define listGetMatchMethod(l) ((l)->match)
|
||||
#define listGetDupMethod(l) ((l)->dup) // 链表复制
|
||||
#define listGetFreeMethod(l) ((l)->free) //链表释放
|
||||
#define listGetMatchMethod(l) ((l)->match) //链表对比
|
||||
|
||||
/* Prototypes */
|
||||
list *listCreate(void);
|
||||
void listRelease(list *list);
|
||||
list *listCreate(void); //链表创建
|
||||
void listRelease(list *list); //释放整个列表
|
||||
void listEmpty(list *list);
|
||||
list *listAddNodeHead(list *list, void *value);
|
||||
list *listAddNodeTail(list *list, void *value);
|
||||
@ -90,7 +106,9 @@ void listRotateHeadToTail(list *list);
|
||||
void listJoin(list *l, list *o);
|
||||
|
||||
/* Directions for iterators */
|
||||
// 从头遍历
|
||||
#define AL_START_HEAD 0
|
||||
// 从尾遍历
|
||||
#define AL_START_TAIL 1
|
||||
|
||||
#endif /* __ADLIST_H__ */
|
||||
|
Loading…
Reference in New Issue
Block a user