Linux双向链表(二)——逻辑判断
来源:互联网 发布:如何查看淘宝客pid 编辑:程序博客网 时间:2024/06/06 00:18
双链表判断函数
1、判断结点是不是链表head的最后一个结点
/** * list_is_last - tests whether @list is the last entry in list @head * @list: the entry to test * @head: the head of the list */static inline int list_is_last(const struct list_head *list,const struct list_head *head){return list->next == head;}
方法很简单:检测 list指向的entry的next结点是否是head,如果是,则返回true,否则,返回false!
用途:对于有序链表,从头遍历链表,需要这样做遍历结束条件
2、判断链表是否为空
/** * list_empty - tests whether a list is empty * @head: the list to test. */static inline int list_empty(const struct list_head *head){return head->next == head;}
有上篇博文可知在链表初始化时,宏定义
#define LIST_HEAD_INIT(name) { &(name),&(name) }
把name的next和prev初始化为指向自身,以表示链表为空。由此,返回head->next == head的逻辑值即可。
返回值:如果链表为空,返回true,否则返回false
用途:操纵一个链表时,首先要保证链表非空,否则,可能导致潜在运行bug
3、不安全方式(并行时)判断链表是否为空
/** * list_empty_careful - tests whether a list is empty and not being modified * @head: the list to test * * Description: * tests whether a list is empty _and_ checks that no other CPU might be * in the process of modifying either member (next or prev) * * NOTE: using list_empty_careful() without synchronization * can only be safe if the only activity that can happen * to the list entry is list_del_init(). Eg. it cannot be used * if another CPU could re-list_add() it. */static inline int list_empty_careful(const struct list_head *head){struct list_head *next = head->next;//局部变量保存,如果未执行return之前,另CPU操作了链表,结果是不正确的,因此在并行架构上要“careful”——小心使用,具体细节看上面代码作者的注释return (next == head) && (next == head->prev);}
4、判断链表是否只有一个结点
/** * list_is_singular - tests whether a list has just one entry. * @head: the list to test. */static inline int list_is_singular(const struct list_head *head) {return !list_empty(head) && (head->next == head->prev);//非空且只有一个,使用图中①②}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>声明<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>> 知识要传播,劳动要尊重! 受益于开源,回馈于社会! 大家共参与,服务全人类!
>> 本博文由my_live_123原创(http://blog.csdn.net/cwcmcw),转载请注明出处!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>^_^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- Linux双向链表(二)——逻辑判断
- 实验二——双向链表
- 双向链表——单链表改进之二
- 详解Linux内核之双向循环链表(二)
- linux shell 逻辑判断
- linux 逻辑语句判断
- linux双向链表
- 作业二 双向链表
- Linux基础数据结构——双向链表
- Linux双向链表(四)——宏
- Linux双向链表(五)——简单使用示例
- [原]linux shell 逻辑判断
- 数据操作——逻辑判断
- 理解Linux双向链表
- 【链表】双向链表——双向循环链表
- Linux(二):巧用分号在命令行执行逻辑判断语句
- nginx源码分析2———基础数据结构二(链表和双向链表)
- 数据结构与算法专题之线性表——链表(二)双向链表
- 在Debian上安装I.MX6及LTIB
- 基于javascript的技术
- 带姓名的成绩单--char5
- 接口
- Android JNI的若干问题总结
- Linux双向链表(二)——逻辑判断
- test1-1(谨以此开始我的c++之旅)
- php smarty
- c#不修改Host文件,指定IP地址
- Android 4.4 webview 架构
- CRM上线之路 走上了CRM实施顾问-第82天上班 -第17周 .
- 超级精仿高仿金山手机卫士源码
- 大端和小端区别-linux
- 【Lua学习笔记】 Lua中实现面向对象,转自云风的博客