Linux内核---60.linux内核链表list的使用
2016-07-25 12:00
453 查看
1.链表的基本操作
初始化链表-->插入链表-->遍历链表-->删除链表中的某一项
下面两个代码一个不用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.代码打包
![](http://blog.chinaunix.net/image/default/fj.png)
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.代码打包
![](http://blog.chinaunix.net/image/default/fj.png)
testlist.rar(下载后改名为testlist.tar.gz)
相关文章推荐
- Linux内核---59.netlink应用层与内核层的交互
- Linux内核---58.kthread与wait_event
- linux基础入门命令
- Linux进程通信(三)IPC信号
- linux之expect
- linux下获取当前屏幕分辨率和当前终端行列数的简单方法
- heartbeat安装
- centos6.7 64位环境下部署MySQL-5.7.13
- Linux查看CPU和内存使用情况【转】
- Linux基础命令
- Linux(centOS6.5)下SVN的安装、配置及开机启动
- linux centos 基本命令
- CentOS下采用Crontab实现PHP脚本定时任务
- Linux之用户身份的查看与切换(su+sudo)
- Linux core 文件介绍
- Linux下设置本地yum安装源
- R第一问 CentOS6.5 修改 /etc/sudoers 提示只读
- Linux源代码目录结构说明
- 菜鸟的Linux苦逼之路4 vim编辑器
- 从本机构建Linux应用程序VHD映像