Linux双向链表(二)——逻辑判断

本文详细介绍了Linux双链表的逻辑判断方法,包括如何判断结点是否为链表末尾、链表是否为空、不安全的空链表判断方式以及如何确定链表仅包含一个结点。这些判断在链表操作中起到关键作用,防止潜在的运行错误。
摘要由CSDN通过智能技术生成

双链表判断函数

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;
}

上篇博文可知在链表初始化时,宏定义

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值