list链表的使用

  1. list_for_each_entry_safe
c 复制代码
list_for_each_entry_safe(pos, n, head, member)

pos:指向当前链表节点的指针,类型为链表元素的结构体类型。

n:指向下一个链表节点的指针,类型与 pos 相同,用于临时保存下一个节点,防止在删除当前节点后丢失链表信息。

head:链表的头节点。

member:链表节点在结构体中的成员名称。

  1. list_for_each_entry
c 复制代码
链表遍历过程的一个封装
#define list_for_each_entry(pos,head,member)\
    for(pos=list_entry((head)->next,typeof(*pos),member);\
        &pos->member!=(head);\
        pos=list_entry(pos->member.next,typeof(*pos),member))

pos:链表中节点的指针

head:链表的头结点指针

member:链表节点中链接前后节点的成员变量名

  1. list_entry
    用于获取包含某个成员变量的结构体指针。
c 复制代码
#define list_entry(ptr,type,member)\
    container_of(ptr,type,member)

ptr:包含成员变量的指针

type:包含成员变量的结构体类型

member:成员变量的名称

  1. 例子
c 复制代码
struct student{
    int id;
    char name[20];
    struct list_head list;
};
struct list_head student_list;//链表头节点
struct student *stu;
list_for_each_entry(stu,&student_list,list) {
    printf("id:%d,name%s\n",stu->id,stu->name);
}
相关推荐
l1t20 小时前
how to build tbox xml into the demo
xml·linux·c语言·parser·tbox
lifallen20 小时前
揭秘KafkaStreams 线程缓存:NamedCache深度解析
数据结构·算法·缓存·kafka·apache
九皇叔叔20 小时前
【2】标识符
c语言
菜就多练,以前是以前,现在是现在21 小时前
Codeforces Round 1048 (Div. 2)
数据结构·c++·算法
失散1321 小时前
分布式专题——9 Redis7底层数据结构解析
java·数据结构·redis·分布式·缓存·架构
林木辛21 小时前
LeetCode 热题 160.相交链表(双指针)
算法·leetcode·链表
野生的编程萌新21 小时前
【C++深学日志】从0开始的C++生活
c语言·开发语言·c++·算法
崎岖Qiu1 天前
leetcode380:RandomizedSet - O(1)时间插入删除和获取随机元素(数组+哈希表的巧妙结合)
java·数据结构·算法·leetcode·力扣·散列表
ZLRRLZ1 天前
【数据结构】图
数据结构·算法·图论
kida_yuan1 天前
【从零开始】13. 数据增强(Data Augmentation)
数据结构·python·nlp