昆明网站建设工作室夜晚必备直播软件
昆明网站建设工作室,夜晚必备直播软件,设计工作室与网站建设工作室,微信app下载安装旧版本以前都是自己写链表或者所用框架都自带链表操作#xff0c;本次工作换了框架没有找到框架自带的链表操作#xff0c;所以尝试使用linux自带的list.h中定义的相关宏和函数写了简单的链表操作#xff0c;竟然踩坑了#xff0c;记录一下。
一、list.h简介
list.h一般放在inc… 以前都是自己写链表或者所用框架都自带链表操作本次工作换了框架没有找到框架自带的链表操作所以尝试使用linux自带的list.h中定义的相关宏和函数写了简单的链表操作竟然踩坑了记录一下。
一、list.h简介
list.h一般放在include/list.h当然有的linux系统放list.h地方在别的地方比如ubuntu。可以使用find / -name list.h找到使用gcc编译的时候include上。
list.h是Linux内核中为了提供统一的链表操作减少结构体的额外开支提供的链表操作。list.h中定义的链表了一个双向循环链表和一个哈希表。
1基本结构
struct list_head {struct list_head *next, *prev;
};2初始化定义
#define LIST_HEAD_INIT(name) { (name), (name) }#define LIST_HEAD(name) \struct list_head name LIST_HEAD_INIT(name)3功能定义
/**添加节点* list_add_tail - add a new entry* new: new entry to be added* head: list head to add it before* * Insert a new entry before the specified head.* This is useful for implementing queues.*/
static inline void list_add_tail(struct list_head *_new, struct list_head *head)
{__list_add(_new, head-prev, head);
}/**删除节点* list_del - deletes entry from list.* entry: the element to delete from the list.* Note: list_empty() on entry does not return true after this, the entry is* in an undefined state.*/
static inline void list_del(struct list_head *entry)
{__list_del(entry-prev, entry-next);entry-next (struct list_head*)LIST_POISON1;entry-prev (struct list_head*)LIST_POISON2;
}/**链表判断* 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;
}4查询定义 /** 直接读取节点的查询方法1* 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_head within the struct.*/
#define 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))/**直接读取节点的查询方法2* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry* pos: the type * to use as a loop cursor.* n: another type * to use as temporary storage* head: the head for your list.* member: the name of the list_head within the struct.*/
#define list_for_each_entry_safe(pos, n, head, member) \for (pos list_entry((head)-next, typeof(*pos), member), \n list_entry(pos-member.next, typeof(*pos), member); \pos-member ! (head); \pos n, n list_entry(n-member.next, typeof(*n), member))
二、实战
程序大概的意思是创建一个5个成员的stu链表打印删除其中一个成员再打印。带参数的可执行程序./test 1标识删除第一个后面数字表示删除第几个。
#include list.h
#include stdlib.h
#include stdio.htypedef struct _ststudent_
{int age;int grade;
}STU_T;typedef struct _stulist_
{STU_T stStu;struct list_head list;
}STU_LIST_T;LIST_HEAD(g_stStuList_Head);
void main(int argc,char *argv[])
{STU_LIST_T *pstStuNULL;STU_LIST_T *pstStu_outNULL;STU_LIST_T *nNULL;int i0;for(i0;i5;i){pstStu(STU_LIST_T*)malloc(sizeof(STU_LIST_T));pstStu-stStu.agei10;pstStu-stStu.gradei100;list_add_tail(pstStu-list,g_stStuList_Head);}printf(----------------add---------------------\n);list_for_each_entry(pstStu_out,g_stStuList_Head,list){printf(pstStu_out-stStu.age%d,grade%d\n,pstStu_out-stStu.age,pstStu_out-stStu.grade);}printf(----------------del---------------------\n);#ifdef TEST_TRUElist_for_each_entry(pstStu_out,g_stStuList_Head,list){if(atoi(argv[1])pstStu_out-stStu.age-10){list_del(pstStu_out-list);free(pstStu_out);continue;}printf(pstStu_out-stStu.age%d,grade%d\n,pstStu_out-stStu.age,pstStu_out-stStu.grade);}#elselist_for_each_entry_safe(pstStu_out,n,g_stStuList_Head,list){if(atoi(argv[1])pstStu_out-stStu.age-10){list_del(pstStu_out-list);free(pstStu_out);continue;}printf(pstStu_out-stStu.age%d,grade%d\n,pstStu_out-stStu.age,pstStu_out-stStu.grade);}
#endifreturn;
} 1为啥不用#include list.h 因为我自己系统带的list.h不在标准库。。
rootubuntu:/home/Ctest/clistTest# find / -name list.h
/lib/firmware/carl9170fw/tools/include/list.h
find: ‘/run/user/1000/gvfs’: Permission denied
/usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-150/include/linux/list.h
/usr/src/linux-headers-5.4.0-150-generic/scripts/kconfig/list.h
/usr/src/linux-headers-5.4.0-150-generic/include/config/system/blacklist/hash/list.h
/usr/src/linux-headers-5.4.0-150-generic/include/config/system/revocation/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-136/scripts/kconfig/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-136/include/linux/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-135/scripts/kconfig/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-135/include/linux/list.h
/usr/src/linux-headers-5.4.0-135-generic/scripts/kconfig/list.h
/usr/src/linux-headers-5.4.0-135-generic/include/config/system/blacklist/hash/list.h
/usr/src/linux-headers-5.4.0-135-generic/include/config/system/revocation/list.h
/usr/src/linux-headers-5.4.0-136-generic/scripts/kconfig/list.h
/usr/src/linux-headers-5.4.0-136-generic/include/config/system/blacklist/hash/list.h
/usr/src/linux-headers-5.4.0-136-generic/include/config/system/revocation/list.h
2编译 gcc -o test listtest.c -I /usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/ -DTEST_TRUE
3-D是为了方便测试 预编译宏。
用来分别编译两个可执行程序说明遇到的坑
4运行结果带参数运行
坑如果只查询使用list_for_each_entry()和list_for_each_entry_safe()都可以但是如果查出来要进行删除操作就需要用list_for_each_entry_safe()否则会宕机。具体原因可以自己查看实现。
rootubuntu:/home/Ctest/clistTest# gcc -o test listtest.c -I /usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/ -DTEST_TRUE
rootubuntu:/home/Ctest/clistTest# ./test 1
----------------add---------------------
pstStu_out-stStu.age10,grade100
pstStu_out-stStu.age11,grade101
pstStu_out-stStu.age12,grade102
pstStu_out-stStu.age13,grade103
pstStu_out-stStu.age14,grade104
----------------del---------------------
pstStu_out-stStu.age10,grade100
pstStu_out-stStu.age593,grade0
Segmentation fault (core dumped)
rootubuntu:/home/Ctest/clistTest# gcc -o test listtest.c -I /usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/
rootubuntu:/home/Ctest/clistTest# ./test 1
----------------add---------------------
pstStu_out-stStu.age10,grade100
pstStu_out-stStu.age11,grade101
pstStu_out-stStu.age12,grade102
pstStu_out-stStu.age13,grade103
pstStu_out-stStu.age14,grade104
----------------del---------------------
pstStu_out-stStu.age10,grade100
pstStu_out-stStu.age12,grade102
pstStu_out-stStu.age13,grade103
pstStu_out-stStu.age14,grade104
rootubuntu:/home/Ctest/clistTest#
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/87946.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!