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.代码打包
testlist.rar(下载后改名为testlist.tar.gz)
阅读(1675) | 评论(0) | 转发(0) |