遍历双向链表
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类型
遍历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类型
相关文章推荐
- 二叉树转换为双向链表(后序遍历)
- 二叉树的前 中 后序遍历,递归和非递归实现以及转换成双向链表
- 遍历所有驱动的名_根据本驱动对象的成员(DriverObject->DriverSection)_对应LDR_DATA_TABLE_ENTRY结构体_双向链表使用
- 双向链表(插入,删除,追加,正反向遍历,查找。。。)
- windbg之使用!list指令遍历双向链表
- C#中遍历对象实例的 属性/值 对
- 遍历HashMap
- 用While自增运算遍历数组
- 利用std::set和遍历骰子的24种可能(人面向骰子)
- 二叉树的定义与遍历
- 无序向量简单操作分析(插入、删除、遍历)
- C# Dictionary用法汇总(定义、遍历、排序)
- 17.二叉树的遍历
- delphi 遍历treeview某个节点下所有节点的函数
- 二叉排序树的创建和遍历与删除指定的节点
- Java中遍历map的代码
- 简单的模拟天猫搜索 并遍历一百页商品(会遇到反爬机制)
- uva-12657 - Boxes in a Line(双向链表)
- 使用Lambda表达式遍历Iterator
- HashMap两种遍历方式的异同(大数据的情况下)