内核链表

删除节点

提取数据结构

list_entry(ptr,type,member) //type 数据结构类型,member是list_head对应的成员名

返回的是节点的指针

已知数据结构中节点指针ptr,找出数据结构,例如:


遍历

list_for_each(struc list_head *pos,struc list_head *head) //记得看实现方法

pos会自动变化,每次循环的时候,它会指向下一个节点

例子:

struct list_head *entry;

struct list_head cs46xx_devs;// 链表头

list_for_each(entry,&cs46xx_devs)

{

card = list_entry(entry,struct cs_card,list);

if(card->dev_midi == minor)

break;

}


例子:

MODULE_ALIAS("List module");
struct student
{
	char name[100];
	int num;
	struct list_head list;
};
struct student *pstudent;
struct student *tmp_student;
struct list_head student_list;
struct list_head *pos;

int mylist_init()
{
	int i=0;
	INIT_LIST_HEAD(&student_list);
	pstudent = kmalloc(sizeof(struct student)*5,GFP_KERNEL);
	memset(pstudent,0,sizeof(struct student)*5);
	for(i=0;i<5;i++)
	{
		sprintf(pstudent[i].name,"Student %d",i+i);
		pstudent[i].num = i+1;
		list_add(&(pstudent[i].list), &student_list);
	}
	list_for_each(pos,&student_list)
	{
		tmp_student = list_entry(pos,struct student,list);
		printk("<0>studnet %d name: %s \n",tmp_student->num,tmp_studnet->name);
	}
	return 0;
}
void mylist_exit()
{
	int i;
	for()
	{
		list_del(&(pstudent[i].list));
	}
	kfree(pstudent);
}
module_inti(mylist_init);
module_exit(mylist_exit);



你可能感兴趣的:(list_for_each,list_entry,list_head,内核链表)