链表
2026/4/26大约 3 分钟基础知识链表数据结构API
链表
一、双向链表函数
os_list_init
os_inline void os_list_init(os_list_node_t *l);功能: 初始化双向链表
os_list_add
os_inline void os_list_add(os_list_node_t *l, os_list_node_t *n);功能: 在表头后添加节点
os_list_add_tail
os_inline void os_list_add_tail(os_list_node_t *l, os_list_node_t *n);功能: 在表尾前添加节点
os_list_del
os_inline void os_list_del(os_list_node_t *n);功能: 删除节点
os_list_del_init
os_inline void os_list_del_init(os_list_node_t *n);功能: 删除节点并重新初始化
os_list_move
os_inline void os_list_move(os_list_node_t *l, os_list_node_t *n);功能: 将节点移动到另一个链表的表头后
os_list_move_tail
os_inline void os_list_move_tail(os_list_node_t *l, os_list_node_t *n);功能: 将节点移动到另一个链表的表尾前
os_list_empty
os_inline os_bool_t os_list_empty(const os_list_node_t *l);功能: 判断链表是否为空
os_list_len
os_inline os_size_t os_list_len(const os_list_node_t *l);功能: 获取链表长度
os_list_splice
os_inline void os_list_splice(os_list_node_t *list, os_list_node_t *l);功能: 合并两个链表(将 list 合并到 l 后)
os_list_splice_tail
os_inline void os_list_splice_tail(os_list_node_t *list, os_list_node_t *l);功能: 合并两个链表到表尾
二、单向链表函数
os_slist_init
os_inline void os_slist_init(os_slist_node_t *l);功能: 初始化单向链表
os_slist_add
os_inline void os_slist_add(os_slist_node_t *l, os_slist_node_t *n);功能: 在表头添加节点
os_slist_add_tail
os_inline void os_slist_add_tail(os_slist_node_t *l, os_slist_node_t *n);功能: 在表尾添加节点
os_slist_del
os_inline void os_slist_del(os_slist_node_t *l, os_slist_node_t *n);功能: 删除节点(需要前驱)
os_slist_del_next
os_inline void os_slist_del_next(os_slist_node_t *l, os_slist_node_t *n);功能: 删除下一个节点
os_slist_empty
os_inline os_bool_t os_slist_empty(const os_slist_node_t *l);功能: 判断链表是否为空
os_slist_len
os_inline os_size_t os_slist_len(const os_slist_node_t *l);功能: 获取链表长度
os_slist_first
os_inline os_slist_node_t *os_slist_first(const os_slist_node_t *l);功能: 获取第一个节点
os_slist_next
os_inline os_slist_node_t *os_slist_next(const os_slist_node_t *n);功能: 获取下一个节点
os_slist_tail
os_slist_node_t *os_slist_tail(const os_slist_node_t *l);功能: 获取最后一个节点
三、宏定义
初始化宏
#define OS_LIST_INIT(name) \
{ &(name), &(name) }
#define OS_SLIST_INIT(name) \
{ OS_NULL }节点操作宏
#define os_list_entry(ptr, type, member) os_container_of(ptr, type, member)
#define os_list_first_entry(head, type, member) os_list_entry((head)->next, type, member)
#define os_list_first_entry_or_null(head, type, member) \
(!os_list_empty(head) ? os_list_first_entry(head, type, member) : OS_NULL)
#define os_list_tail_entry(head, type, member) os_list_entry(os_list_tail(head), type, member)
#define os_list_tail_entry_or_null(head, type, member) \
(!os_list_empty(head) ? os_list_tail_entry(head, type, member) : OS_NULL)
#define os_slist_entry(ptr, type, member) os_container_of(ptr, type, member)
#define os_slist_first_entry(head, type, member) os_slist_entry((head)->next, type, member)
#define os_slist_first_entry_or_null(head, type, member) \
(!os_slist_empty(head) ? os_slist_first_entry(head, type, member) : OS_NULL)
#define os_slist_tail_entry(head, type, member) os_slist_entry(os_slist_tail(head), type, member)
#define os_slist_tail_entry_or_null(head, type, member) \
(!os_slist_empty(head) ? os_slist_tail_entry(head, type, member) : OS_NULL)遍历宏
#define os_list_for_each(pos, head) for ((pos) = (head)->next; (pos) != (head); (pos) = (pos)->next)
#define os_list_for_each_safe(pos, n, head) \
for ((pos) = (head)->next, (n) = (pos)->next; (pos) != (head); (pos) = (n), (n) = (pos)->next)
#define os_list_for_each_entry(pos, head, type, member) \
for ((pos) = os_list_entry((head)->next, type, member); &(pos)->member != (head); \
(pos) = os_list_entry((pos)->member.next, type, member))
#define os_list_for_each_entry_safe(pos, n, head, type, member) \
for ((pos) = os_list_entry((head)->next, type, member), (n) = os_list_entry(pos->member.next, type, member); \
&(pos)->member != (head); \
(pos) = (n), (n) = os_list_entry(n->member.next, type, member))
#define os_slist_for_each(pos, head) for (pos) = (head)->next; (pos) != OS_NULL; (pos) = (pos)->next)
#define os_slist_for_each_safe(pos, n, head) \
for (pos) = (head)->next, n) = (pos) != OS_NULL ? (pos)->next : OS_NULL; (pos) != OS_NULL; \
(pos) = n), n) = (pos) != OS_NULL ? (pos)->next : OS_NULL)四、结构体
双向链表节点
struct os_list_node
{
struct os_list_node *next; /* Point to next node */
struct os_list_node *prev; /* point to previous node */
};
typedef struct os_list_node os_list_node_t;单向链表节点
struct os_slist_node
{
struct os_slist_node *next; /* Point to next node */
};
typedef struct os_slist_node os_slist_node_t;