Linux内核---60.linux内核链表list的使用
来源:互联网 发布:学白话软件 编辑:程序博客网 时间:2024/05/02 12:26
1.链表的基本操作
初始化链表-->插入链表-->遍历链表-->删除链表中的某一项
下面两个代码一个不用entry,一个用entry,还是用entry的比较简单明了
2.代码->testlist1.c
3. 代码->testlist2.c
注:判断链表为空
printk(KERN_ALERT "list_empty=%d\n", list_empty(&head.list));
为空时会打印1, 不为空时会打印0
4.代码打包
testlist.rar(下载后改名为testlist.tar.gz)
初始化链表-->插入链表-->遍历链表-->删除链表中的某一项
下面两个代码一个不用entry,一个用entry,还是用entry的比较简单明了
2.代码->testlist1.c
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/slab.h>
- #define LIST_LEN 10
- typedef struct __num_node_{
- int num;
- struct list_head list;
- }num_node;
- num_node head;
- static int __init hello_init(void)
- {
- int i;
- -->不用entry时,既需要定义struct list_head也需要定义num_node
- struct list_head* pos;
- struct list_head* n;
- num_node* listnode;
- num_node* p;
- //1.初始化链表
- INIT_LIST_HEAD(&head.list); -->初始化双向链表
- //2.向链表中添加项
- printk(KERN_ALERT "add node 1-10:\n");
- for(i=0; i<LIST_LEN; i++)
- {
- listnode = (num_node*)kmalloc(sizeof(num_node), GFP_KERNEL);
- listnode->num = i;
- list_add_tail(&listnode->list, &head.list); -->只是把list插入到链表中,而数据本身不会插入到链表中
- printk(KERN_ALERT "add node=%d\n", i);
- }
- printk(KERN_ALERT "\n");
- //3.遍历链表
- printk(KERN_ALERT "node traversal 1111:\n");
- list_for_each(pos, &head.list)
- {
- p = list_entry(pos, num_node, list);
- printk(KERN_ALERT "node data=%d\n", p->num);
- }
- printk(KERN_ALERT "\n");
- //4.删除链表中的某一项
- printk(KERN_ALERT "node delete 3:\n");
- list_for_each_safe(pos, n, &head.list)
- {
- p = list_entry(pos, num_node, list);
- if(3 == p->num)
- {
- printk(KERN_ALERT "find node -> 3\n");
- list_del(&p->list);
- kfree(p);
- }
- //printk(KERN_ALERT "node data=%d\n", p->num);
- }
- printk(KERN_ALERT "\n");
- //5.再次遍历链表
- printk(KERN_ALERT "node traversal 2222:\n");
- list_for_each(pos, &head.list)
- {
- p = list_entry(pos, num_node, list);
- printk(KERN_ALERT "node data=%d\n", p->num);
- }
- printk(KERN_ALERT "\n");
-
- return 0;
- }
- static void __exit hello_exit(void)
- {
- printk(KERN_ALERT "goodbye my first dirver\n");
- return ;
- }
- module_init(hello_init);
- module_exit(hello_exit);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("wangcong");
3. 代码->testlist2.c
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/slab.h>
- #define LIST_LEN 10
- typedef struct __num_node_{
- int num;
- struct list_head list;
- }num_node;
- num_node head;
- static int __init hello_init(void)
- {
- int i;
- num_node* listnode;
- num_node* p; -->这个地方就不需要struct list_head了
- num_node* n; -->这个地方就不需要struct list_head了
- //1.初始化链表
- INIT_LIST_HEAD(&head.list);
- //2.向链表中添加项
- printk(KERN_ALERT "add node 1-10:\n");
- for(i=0; i<LIST_LEN; i++)
- {
- listnode = (num_node*)kmalloc(sizeof(num_node), GFP_KERNEL);
- listnode->num = i;
- list_add_tail(&listnode->list, &head.list);
- printk(KERN_ALERT "add node=%d\n", i);
- }
- printk(KERN_ALERT "\n");
- //3.遍历链表
- printk(KERN_ALERT "node traversal 1111:\n");
- list_for_each_entry(p, &head.list, list)
- {
- printk(KERN_ALERT "node data=%d\n", p->num);
- }
- printk(KERN_ALERT "\n");
- //4.删除链表中的某一项
- printk(KERN_ALERT "node delete 4:\n");
- list_for_each_entry_safe(p, n, &head.list, list)
- {
- if(4 == p->num)
- {
- printk(KERN_ALERT "find node -> 4\n");
- list_del(&p->list);
- kfree(p);
- }
- //printk(KERN_ALERT "node data=%d\n", p->num);
- }
- printk(KERN_ALERT "\n");
- //5.再次遍历链表
- printk(KERN_ALERT "node traversal 2222:\n");
- list_for_each_entry(p, &head.list, list)
- {
- printk(KERN_ALERT "node data=%d\n", p->num);
- }
- printk(KERN_ALERT "\n");
-
- return 0;
- }
- static void __exit hello_exit(void)
- {
- printk(KERN_ALERT "goodbye my first dirver\n");
- return ;
- }
- module_init(hello_init);
- module_exit(hello_exit);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("wangcong");
printk(KERN_ALERT "list_empty=%d\n", list_empty(&head.list));
为空时会打印1, 不为空时会打印0
4.代码打包
testlist.rar(下载后改名为testlist.tar.gz)
0 0
- Linux内核---60.linux内核链表list的使用
- Linux 内核list使用
- linux list 内核链表
- linux内核list模块的使用
- 用户空间使用linux内核链表list编程
- list.h linux内核链表分析
- linux内核list.h中对链表的操作
- 【Linux内核数据结构】最为经典的链表list
- linux内核链表使用
- linux内核链表使用
- linux内核链表使用
- linux内核链表使用
- linux内核list实现
- linux内核list解析
- linux内核list.h
- linux内核中的list
- linux内核list.h
- Linux内核通用链表<linux/list.h>阅读
- Linux内核---59.netlink应用层与内核层的交互
- Android产品研发(十九)-->Android studio中的单元测试
- poj 2063
- IIS简易配置
- 微信开发 access_token 缓存方法
- Linux内核---60.linux内核链表list的使用
- iOS runLoop 用法详解3
- javascript_nodejs_express
- Linux内核---61.linux内核静态库的编译与使用
- 【C/C++】左旋字符串
- to_param()函数和parameterize()函数
- 不错的视频播放组件
- Android Activity 生命周期
- 侯捷谈Java反射机制