list/list.h

47 lines
1.4 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef LIST_H
#define LIST_H
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
typedef struct list_node_t
{
void *data;
struct list_node_t *next;
} list_node_t;
typedef list_node_t *list_t;
typedef enum list_error
{
LIST_OK = 0,
LIST_ERR_LIST_IS_NULL = 1,
LIST_ERR_MALLOC_FAILED,
} list_error;
/**
* @brief 创建一个节点如果list为NULL则创建一个空链表否则创建一个节点并添加到链表尾部
* @param list 链表指针
* @param data 数据指针
*/
int list_node_add(list_t *list, void *data);
/**
* @brief 从链表中删除节点
* @param list 链表指针
* @param data 数据指针
*/
int list_node_remove(list_t *list, void *data);
int list_size(list_t *list);
/**
* @param list 链表
* @param data 要添加的数据不要取地址,在宏定义里面自动取地址
* @warning 这种方式增加节点,会导致无法确定节点添加的先后顺序,更换编译环境后可能会影响编译效率
*/
#define EXPORT_LIST_NODE(list, data) \
int __attribute__((constructor)) export_list_node_##list_##data(void) \
{ \
return list_node_add(&list, (void *)&data); \
}
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // LIST_H