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

/***********************************
文件名:kernel link list of linux.h
作者:Bumble Bee
日期:2015-1-31
功能:移植linux内核链表
************************************/ /*链表结点数据结构*/
struct list_head
{
struct list_head *next, *prev;
}; /***********************************
函数名: INIT_LIST_HEAD
参数: 指向list_head结构体的指针
返回值: 无
函数功能:通过将前向指针和后向指
针指向自己来创建一个链表表

***********************************/
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
} /***********************************
函数名: __list_add
参数: @new:要插入结点的指针域
@prev:前一个节点的指针域
@next:后一个节点的指针域
返回值: 无
函数功能:在两个已知节点中插入新节点
***********************************/ static inline void __list_add(struct list_head *new,
struct list_head *prev, struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
extern void __list_add(struct list_head *new,
struct list_head *prev, struct list_head *next); /**************************************
函数名: list_add
参数: @new:要插入结点的指针域
@head:要插入链表表头的指针域
返回值: 无
函数功能:在已知链表头部插入新节点
**************************************/ static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
} /**************************************
函数名: list_add_tail
参数: @new:要插入结点的指针域
@head:要插入链表表头的指针域
返回值: 无
函数功能:在已知链表尾部插入新节点
**************************************/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
} /*************************************
函数名: list_for_each
参数: @pos:遍历链表的光标
@head:要遍历链表的表头
返回值: 无
函数功能:实质为一个for循环,遍历链表
*************************************/
#define list_for_each(pos, head) \
for (pos = (head)->next;pos != (head); \
pos = pos->next) /*************************************************
函数名: list_entry
参数: @ptr:节点中list_head的地址
@type:节点的类型
@member:list_head 在结构体中成员的名字
返回值: 节点的地址,已被强制转化为type型指针
函数功能:将节点最低位置假设为0,此时取成员member
的地址即为offset,再用list_head的地址将
offset减去即为节点的地址
**************************************************/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member) #define container_of(ptr, type, member) ({ \
const typeof(((type *))->member) * __mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); }) #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) static inline void __list_del(struct list_head *prev, struct list_head *next)
{
next->prev = prev;
prev->next = next;
}
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
}

二、设计应用程序测试链表

/****************************
文件名:homework.c
作者:Bumble Bee
日期:2015-1-31
功能:测试移植的linux内核链表
*****************************/ #include <stdio.h>
#include "kernel link list of linux.h" struct score
{
int num;
int english;
int math;
struct list_head list;
}; struct score stu1,stu2,stu3,*temp; struct list_head score_head,*pos; int main()
{
INIT_LIST_HEAD(&score_head); //创建链表函数 stu1.num = ;
stu1.english = ;
stu1.math = ;
list_add_tail(&(stu1.list),&(score_head)); stu2.num = ;
stu2.english = ;
stu2.math = ;
list_add_tail(&(stu2.list),&(score_head)); stu3.num = ;
stu3.english = ;
stu3.math = ;
list_add_tail(&(stu3.list),&(score_head)); list_del(&(stu2.list)); list_for_each(pos,&(score_head))
{
temp = list_entry(pos,struct score,list);
printf("No %d,english is %d,math is %d\n",temp->num,temp->english,temp->math);
} return ; }

三、运行结果

  

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

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

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

  2. 第32课 Linux内核链表剖析

    1. Linux内核链表的位置及依赖 (1)位置:{linux-2.6.39}\\include\linux\list.h (2)依赖 ①#include<linux\types.h> ② ...

  3. linux内核链表使用

    原文链接:http://blog.csdn.net/xnwyd/article/details/7359373 Linux内核链表的核心思想是:在用户自定义的结构A中声明list_head类型的成员p ...

  4. 数据结构开发(10):Linux内核链表

    0.目录 1.老生常谈的两个宏(Linux) 1.1 offsetof 1.2 container_of 2.Linux内核链表剖析 3.小结 1.老生常谈的两个宏(Linux) Linux 内核中常 ...

  5. linux内核链表剖析

    1.移植linux内核链表,使其适用于非GNU编译器 2.分析linux内核中链表的基本实现 移植时的注意事项 清除文件间的依赖 剥离依赖文件中与链表实现相关的代码 清除平台相关的代码(GNU C) ...

  6. Linux内核链表——看这一篇文章就够了

    本文从最基本的内核链表出发,引出初始化INIT_LIST_HEAD函数,然后介绍list_add,通过改变链表位置的问题引出list_for_each函数,然后为了获取容器结构地址,引出offseto ...

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

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

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

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

  9. Linux 内核链表

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

随机推荐

  1. UITableViewStyleGrouped顶部留白问题

    我这样创建, ? 1 2 self.tableView = [[UITableView alloc] initWithFrame:                       CGRectMake(0 ...

  2. C#中将图片文件转化为二进制数组-用于数据库存储

    在项目开发中,使用SQL Server存储数据,数据类型image可以保存图片.但是在存储之前需要将图片转化为二进制数组的形式进行赋值. 将图片文件转换为二进制数组 /// <summary&g ...

  3. js判断访问者是否来自移动端代码

    <script type="text/javascript"> function is_mobile() { var regex_match = /(nokia|iph ...

  4. 昨天mac更新后,网络又出问题了。。。

    情况如图...

  5. 微信js-sdk,选择图片,上传,下载到本地,php服务端

    //前端js代码<script> //客户端6.0.2 wx.config({ //debug:true, appId: "{pigcms:$signPackage.appId} ...

  6. Visual Studio使用技巧记录

    1.关闭调试,iis express仍显示在托盘中: 工具 ---> 选项 ---> 调试 ---> 编辑并继续,取消选择“编辑并继续”的选择框 2.关闭浏览器一直请求: 在调试旁边 ...

  7. wsdl和wadl区别

    [转]http://blog.csdn.net/liuxiao723846/article/details/51611183 1.Java开发WebService最重要的两个规范: JSR-224 ( ...

  8. Class类相关

    Class类是java.lang包中的类,该类的实例可以帮助程序创建其他类的实例或者取得其他类的对象的内部信息 使用class类获得一个类相关的class类(注意得到的是class类,不是相关的类) ...

  9. SQL中删除同一字段中重复的值

    /////////////////////目地:ZDJZ_DIS中 name字段有重复的值,删除重复的值 DELETE * FROM ZDJZ_DIS WHERE NAME IN (select NA ...

  10. 关于常用却忘记的css,jQuery

    text-indent:35px;//首行缩进 line-height:12px;//行高,高度和外层高度一样就会居中 box-shadow:inset 0px 0px 2px #ccc; conte ...