您的位置:首页 > 其它

遍历双向链表

2010-11-05 17:37 120 查看
一 list_for_each()

遍历head(struct list_head)索引的双向链表,没找到一个节点就将该节点赋给pos(struct list_head)

#define list_for_each(pos, head) /

for (pos = (head)->next; prefetch(pos->next), pos != (head); pos = pos->next)

pos ---+

|

head -->|---------------| | -
|---------------| |---------------|

|list_head *next|-+->|list_head *next|--->|list_head *next|

|list_head *prev|<---|list_head *prev|<---|list_head *prev|

|---------------| |---------------| |---------------|

二 list_entry()

#define list_entry(ptr, type, member) ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

我们使用list_entry()宏在linux链表中访问链表数据。

  原理为指针ptr指向结构体type中的成员member;通过指针ptr,返回结构体type的起始地址。

  定义中((size_t) &(type *)0)->member)意为:把0地址转化为type结构的指针,然后获取该结构中member成员的指针,并将其强制转换为size_t类型
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: