# Data Structure And Algorithm ：C language（list）

## 链表

#### 1. 单链表（list）：

``````typedef struct ListElmt_  //元素的定义
{
void *data;
struct ListElmt_ *next;
} ListElmt;

typedef struct List_  //单链表的定义
{
int size;
int (*match)(const void *key1, const void *key2);  //match不由链表使用，而是由链表派生而来的新类型使用
void (*destroy)(void *data);  //想象为析构函数
ListElmt *tail;
} List;
``````

``````void list_init(List *list, void (*destroy)(void *data));
void list_destory(List *list);
int list_ins_next(List *list, ListElmt *element, const void *data);
int list_rem_next(List *list, ListElmt *element, void **data);
#define list_size(list) ((list)->size)
#define list_tail(list) ((list)->tail)
#define list_is_tail(list, element) ((element) == (list)->tail ? 1 : 0)
#define list_data(element) ((element)->data)
#define list_next(element) ((element)->next)
``````

1.插入链表头部
2.插入其他位置

#### 2.双向链表（dlist）：

``````typedef struct DListElmt_
{
void *data;
struct DListElmt_ *prev;
struct DListElmt_ *next;
} DListElmt;

typedef struct DList_
{
int size;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
DListElmt *tail;
} DList;
``````

``````void dlist_init(DList *list, void (*destroy)(void *data));
void dlist_destroy(DList *list);
int dlist_ins_next(DList *list, DListElmt *element, const void *data);
int dlist_ins_prev(DList *list, DListElmt *element, const void *data);
int dlist_remove(DList *list, DListElmt *element, void **data);
#define dlist_size(list) ((list)->size)
#define dlist_tail(list) ((list)->tail)
#define dlist_is_head(element) ((element)->prev == NULL ? 1 : 0)
#define dlist_is_tail(element) ((element)->next == NULL ? 1 : 0)
#define dlist_data(element) ((element)->data)
#define dlist_next(element) ((element)->next)
#define dlist_prev(element) ((element)->prev)
``````

#### 3. 循环链表

``````typedef struct CListElmt_
{
void *data;
struct CListElmt_ *next;
} CListElmt;

typedef struct CList_
{
int size;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
} CList;
``````

``````void clist_init(CList *list, void (*destroy)(void *data));
void clist_destory(CList *list);
int clist_ins_next(CList *list, CListElmt *element, const void *data);
int clist_rem_next(CList *list, CListElmt *element, void **data);
#define clist_size(list) ((list)->size)