一、
   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. 苹果iOS操作系统整体架构层次讲解

     iOS的系统架构分为四个层次:核心操作系统层(Core OS layer).核心服务层(Core Services layer).媒体层(Media layer)和可触摸层(Cocoa Touch ...

  2. jQuery实现密保互斥问题

    密保互斥问题: 密保通常都会有n个问题,让用户选择其中2.3个,而且都不会让用户选择重复的问题.这就要求密保互斥. 效果如下: 下面我用了jquery实现密保互斥,用于解决密保,投票等类似互斥问题,可 ...

  3. [转] Console命令详解,让调试js代码变得更简单

    http://www.cnblogs.com/see7di/archive/2011/11/21/2257442.html Firebug是网页开发的利器,能够极大地提升工作效率. 但是,它不太容易上 ...

  4. Android 交错 GridView

    原文地址 本文演示在你的 Android 应用程序中显示交错 GridView(Staggered GridView ). 下载 Demo 交错 GridView 交错 GridView 只是具有不等 ...

  5. 【字符串匹配】UVALive 4670 模板题

    给一个文本T,和n个模板字符串,都是由小写字母组成,问这些字符串那些在字符串中出现的次数最多,输出最多的次数以及相应的字符串. AC自动机的模板题,递归输出的时候改成累加次数统计数组cnt即可. 大白 ...

  6. 关于HttpServlet和Servlet以及doPost和doGet关系

    这两天在看Servlet和Jsp,spring太难了,还是先看看基础,只怪自己太弱了. Servlet是一个接口,本身定义的是一种网络服务,HttpServlet是已经实现了Servlet接口,也就是 ...

  7. 汉字转拼音(pinyin4j-2.5.0.jar)

    import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...

  8. SQL从入门到基础–03 SQLServer基础1(主键选择、数据插入、数据更新)

    一.SQL语句入门 1. SQL语句是和DBMS“交谈”专用的语句,不同DBMS都认SQL语法. 2. SQL语句中字符串用单引号. 3. SQL语句中,对于SQL关键字大小写不敏感,对于字符串值大小 ...

  9. java静态代码块 类加载顺序问题。

    class B extends Object { static {System.out.println("Load B");} public B(){System.out.prin ...

  10. Dapper基本增删改查

    说明: 1.在using语句块中不用dbConnection.Open(),因为Execute方法中会Open,并且在执行完成会Close. 2.在Ado.Net中要手动Open,在Using语句块中 ...