最近在研究redis源码,顺便复习一下数据结构,感觉好多都忘了,下面代码是redis-3.0.4里面adlist.c和adlist.h的部分源码,部分给摘除出来了,方便以后使用,如果错误或者其他可以优化的地方希望大家提出来
#ifndef __LIST_H__
#define __LIST_H__
typedef struct listNode
{
struct listNode *next;
void *value;
} listNode;
typedef struct list
{
listNode *head;
listNode *tail;
void *(*dup)(void *ptr);
void (*free)(void *ptr);
int (*match)(void *ptr, void *key);
void (*print)(void *ptr);
void (*printAll)(void *list);
unsigned long len;
} list;
#define listLength(l) ((l)->len)
list *listCreate(void);
list *listAddNodeHead(list *list, void *value);
void listRelease(list *list);
listNode *listSearchKey(list *list, void *key);
void listDelNode(list *list, listNode *node);
#endif
源文件
#include "list.h"
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
void Print(void *value)
{
printf("%d ",*(int*)(value));
}
void printAll(list *list)
{
if(listLength(list))
{
listNode *temp = list->head;
while(temp)
{
Print(temp->value);
printf(" \n");
temp = temp->next;
}
}
}
int match(void *ptr, void *key)
{
return *(int*)ptr == *(int*)key ? 1 : 0;
}
list *listCreate(void)
{
struct list *list;
if ((list = malloc(sizeof(*list))) == NULL)
return NULL;
list->head = list->tail = NULL;
list->len = 0;
list->dup = NULL;
list->free = NULL;
list->match = NULL;
list->print = NULL;
list->printAll = NULL;
return list;
}
list *listAddNodeHead(list *list, void *value)
{
listNode *node;
if ((node = malloc(sizeof(*node))) == NULL)
{
return NULL;
}
node->value = value;
node->next = NULL;
if (!listLength(list))
{
list->head = list->tail = node;
node->next = NULL;
}
else
{
node->next = list->head;
list->head = node;
}
list->len++;
return list;
}
void listRelease(list *list)
{
unsigned int len;
listNode *current,*next;
current = list->head;
len = listLength(list);
while(len--)
{
next = current->next;
if(list->free)
{
list->free(current->value);
}
free(current);
current = next;
}
free(list);
}
listNode *listSearchKey(list *list, void *key)
{
if(!list || !listLength(list))
{
return NULL;
}
listNode *current = list->head;
int len = listLength(list);
while(len--)
{
if(list->match)
{
if(list->match(current->value,key))
{
return current;
}
}
else
{
if(current->value == key)
{
return current;
}
}
current = current->next;
}
return NULL;
}
void listDelNode(list *list, listNode *node)
{
if(!listLength(list) || !node)
{
return;
}
if(list->free)
{
list->free(node->value);
node->value = NULL;
}
listNode *current = list->head->next;
if(node == list->head)
{
free(list->head);
list->head = current;
list->len--;
return;
}
current = list->head->next;
listNode *pre = list->head;
while(current)
{
if(current == node)
{
pre->next = current->next;
current->next = NULL;
free(current);
list->len--;
return;
}
pre = current;
current = current->next;
}
}
int main()
{
list *list = listCreate();
list->free = free;
list->print = Print;
list->printAll = printAll;
list->match = match;
int i = 0;
while(i++ < 100)
{
int *p = (int*)malloc(sizeof(int));
*p = i;
list = listAddNodeHead(list,p);
}
int val = 1;
listNode *node = listSearchKey(list,&val);
listDelNode(list,node);
list->printAll(list);
listRelease(list);
return 0;
}
陆续会继续更新,主要移植redis-3.0.4算法部分,希望大家提出宝贵的意见