2025-01-11 16:25:37 +00:00
|
|
|
|
#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;
|
2024-11-20 15:03:26 +00:00
|
|
|
|
|
2025-01-11 16:25:37 +00:00
|
|
|
|
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);
|
2024-11-20 15:03:26 +00:00
|
|
|
|
|
2025-01-11 16:25:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* @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
|