Linux 中链表的使用方式
        【摘要】     本文介绍Linux 中链表的使用方式 
1.介绍 
必须包含头文件<linux/list.h>,该文件定义了一个简单的list_head类型的结构体;  c  struct list_head {  struct list_head *next, *prev;  };  实际代码的链表几乎都是结构体类型构成,每个结构体描述链表中的一项。需要使用链表只需要在结...
    
    
    
    本文介绍Linux 中链表的使用方式
1.介绍
- 必须包含头文件<linux/list.h>,该文件定义了一个简单的list_head类型的结构体;
 c
 struct list_head {
 struct list_head *next, *prev;
 };
 
- 实际代码的链表几乎都是结构体类型构成,每个结构体描述链表中的一项。需要使用链表只需要在结构体里嵌入一个list_head: 
 c
 struct my_struct{
 struct list_hand list;
 ....
 void *my_data;
 }
 
- 链表在使用前必须用INIT_LIST_HEAD来初始化;
 c
 static inline void INIT_LIST_HEAD(struct list_head *list)
 {
 list->next = list;
 list->prev = list;
 }
 
- 头文件 - <linux/list.h>中声明了list的相关操作- /* 在链表头部添加新项,通常是链表头部,这样可以用来建立栈 */ void list_add(struct list_head *new, struct list_head *head); /* Insert a new entry before the specified head. This is useful for implementing queues. */ void list_add_tail(struct list_head *new, struct list_head *head); /* deletes entry from list. 删除列表中的指定项 */ void list_del(struct list_head *entry); /* deletes entry from list and reinitialize it.删除列表中的指定项,并初始它(有可能插入到其他链表中) */ void list_del_init(struct list_head *entry); /* tests whether a list is empty.如果链表为空,返回非零值 */ int list_empty(const struct list_head *head); /** * iterate over a list,遍历链表 * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. */ list_for_each(pos, head); /** * list_for_each_entry - iterate over list of given type * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list_struct within the struct. */ 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))- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
 
2. 使用步骤
- 定义一个包含struct list_head xxx的结构体,并实例化一个对象;
- 使用INIT_LIST_HEAD()初始化链表;
- 使用list_add()等函数向链表中添加内容;
- list_for_each_entry()遍历链表或- list_del()删除指定项;
文章来源: xuesong.blog.csdn.net,作者:内核笔记,版权归原作者所有,如需转载,请联系作者。
原文链接:xuesong.blog.csdn.net/article/details/81912611
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        
        
        - 点赞
- 收藏
- 关注作者
 
             
           
评论(0)