linux内核链表:
链表通常包括两个域:数据域和指针域。
struct list_head{
struct list_head *next,*prev;
};
include/linux/list.h中实现了一套精彩的链表数据结构。
传统的链表指针指向下一个节点的头部。linux链表指针指向下一个指针list_head结构(*next),双向循环。不会随着外部数据的变化而变化,使它具有通用性。?

-------------------------------------------------------------------

linux内核提供的链表主要操作:
1.初始化链表头:
INIT_LIST_HEAD(list_head *head):将*head,*prev指向list本身。
2.插入节点
list_add(struct list_head *new,struct list_head *head)
list_add_tail(struct list_head *new,struct list_head *head)
3.删除节点
list_del(struct list_head *entry:一个指针结构体)
4.提取数据结构
list_entry(ptr(指向list_head的指针),type(外部结构的类型),member(struct list_head对应的成员名))
已知数据结构中的节点指针ptr,找出数据结构,例
list_entry(aup,struct,autofs,list)
5.遍历
list_for_each(struct list_head *pos,struct list_head *head(你所需要遍历的链表的链表头))
如:
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;
}
看内核代码看list_entry是如何实现的???

----------------------------------------------------------

内核链表的一个具体实现实例:

#include<linux/list.h>

#include<linux/module.h>

#include<linux/kernel.h>

#include<linux/init.h>

#include<linux/slab.h>  //kfree kmalloc

MODULE_LICENSE("GPL");//(general public license)
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+1);
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>student%d name:%s\n",tmp_student->num,tmp_student->num,tmp_student->name);
}
return 0;
}//遍历学生信息
void mylist_exit()
{
int i;
for(i=0;i<5;i++){
list_del(&(pstudent[i].list));

}
kfree(pstudent);
}
module_init(mylist_init);
module_exit(mylist_exit);

linux内核链表的使用的更多相关文章

  1. C语言 Linux内核链表(企业级链表)

    //Linux内核链表(企业级链表) #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...

  2. 深入分析 Linux 内核链表--转

    引用地址:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 一. 链表数据结构简介 链表是一种常用的组织有序数据 ...

  3. Linux 内核链表

    一 . Linux内核链表 1 . 内核链表函数 1.INIT_LIST_HEAD:创建链表 2.list_add:在链表头插入节点 3.list_add_tail:在链表尾插入节点 4.list_d ...

  4. linux内核链表分析

    一.常用的链表和内核链表的区别 1.1  常规链表结构        通常链表数据结构至少应包含两个域:数据域和指针域,数据域用于存储数据,指针域用于建立与下一个节点的联系.按照指针域的组织以及各个节 ...

  5. 深入分析 Linux 内核链表

    转载:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/   一. 链表数据结构简介 链表是一种常用的组织有序数据的数据结构,它通过指 ...

  6. Linux 内核 链表 的简单模拟(2)

    接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the & ...

  7. Linux 内核 链表 的简单模拟(1)

    第零章:扯扯淡 出一个有意思的题目:用一个宏定义FIND求一个结构体struct里某个变量相对struc的编移量,如 struct student { int a; //FIND(struct stu ...

  8. linux内核链表的移植与使用

    一.  Linux内核链表为双向循环链表,和数据结构中所学链表类似,具体不再细讲.由于在内核中所实现的函数十分经典,所以移植出来方便后期应用程序中的使用. /********************* ...

  9. [国嵌攻略][108][Linux内核链表]

    链表简介 链表是一种常见的数据结构,它通过指针将一系列数据节点连接成一条数据链.相对于数组,链表具有更好的动态性,建立链表时无需预先知道数据总量,可以随机分配空间,可以高效地在链表中的任意位置实时插入 ...

随机推荐

  1. 零基础教你写python爬虫

    大家都知道python经常被用来做爬虫,用来在互联网上抓取我们需要的信息. 使用Python做爬虫,需要用到一些包: requests urllib BeautifulSoup 等等,关于python ...

  2. 地图API地址  百度地图开放平台

    http://lbsyun.baidu.com/index.php?title=jspopular

  3. h5学习笔记:vuethink 配置

    vuethink 是一款基于PHP TP5和Vuejs 结合的后台框架,设计起来是使用较为前沿.在使用的过程,需要对这款开源的后台做一些调整和面对一些细节的坑.前段时间也因为有项目需求,所以下载了玩了 ...

  4. 字符串API

    string可以看成是多个字符组成的只读数组,也可以通过下标去访问某个字符 访问i位置的字符 :  str[i] 字符个数:  str.length 倒数第n个字符 : str[str.length- ...

  5. 急!!ftp登录错误,提示 530 not logged in,连接失败 ,,是怎么回事啊

    愤怒地青鸟 | 浏览 68533 次 发布于2017-04-11 00:44 最佳答案 核心提示:Ftp登陆不了是很经常碰到的事,很多人常常是不加分析就发贴询问.老实说,这样既浪费自己时间,又浪费别人 ...

  6. ubuntu-apache下隐藏thinkphp入口文件index.php

    按照thinkphp手册中来讲,apache服务器下,隐藏thinkphp入口文件有3步: httpd.conf配置文件中加载了mod_rewrite.so模块 AllowOverride None ...

  7. salesforce零基础学习(八十一)更改标准字段的label名称(Admin)

    我们在开发中往往需要考虑国际化功能,salesforce 提供了国际化功能,在search部分搜索translate,便可以找到translate部分,从而对需要的进行translate.比如pick ...

  8. 新手数据比赛中数据处理方法小结(python)

    第一次参加,天池大数据竞赛(血糖预测),初赛排名1%.因为自己对python不熟悉,所以记录一下在比赛中用到的一些python方法的使用(比较基础细节,大佬绕道): 1.数据初探 data.info( ...

  9. 开始学习 Backbone

    [转]开始学习 Backbone 如何将模型-视图-控制器 (MVC) 架构引入 Ajax Web 应用程序 如何高效管理 Web 应用程序中的数目众多的 JavaScript 代码行是一个挑战.As ...

  10. python_如何使用临时文件

    案例: 某项目中,从传感器中获得采集数据,每收集到1G的数据后做是数据分析,最终只保留数据分析的结果,收集到的数据放在内存中,将会消耗大量内存,我们希望把这些数据放到一个临时的文件中 临时文件不能命名 ...