一、
   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. Android各种颜色dawable.xml中定义

    < drawable name="white">#FFFFFF< /drawable>< !--白 --> < drawable name ...

  2. [转] PostgreSQL的时间/日期函数使用

    PS:http://blog.csdn.net/love_rongrong/article/details/6712883 字符串模糊比较 日期类型的模糊查询是不能直接进行的,要先转换成字符串然后再查 ...

  3. 人工智能2:智能Agent

    一.Agent基本定义 基于理性行为的Agent是本书人工智能方法的核心.Agent由传感器.执行器两个重要元件组成,具有与环境交互的能力,其能力是通过分析感知序列,经过Agent函数映射到相应的行动 ...

  4. sharepoint查询超出阈值

    昨天客户出了webpart显示数据不稳定的bug,经过这两天的艰苦排查终于发现了是列表视图阈值造成的问题,经过在网上搜索终于找到了类似的解决方法. SPQuery query = new SPQuer ...

  5. 在SPItemEventReceiver中使用BeforeProperties和AfterProperties

    当你利用这些事件时,就很快会发现存在前(同步)后(异步)两种事件.其方法的后缀分别为“ing”(比如,ItemAdding)和“ed”(比如,ItemAdded),分别代表了变更发生前调用和发生后调用 ...

  6. Wpf解决TextBox文件拖入问题、拖放问题

    在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同, 解放方法如下: 使用Previ ...

  7. 注意mysql中的编码格式和php中的编码格式一致

    今天发现用php代码插入英文可以,但是中文插入不进去,注意编码要一致,@mysql_connect("localhost","root","12345 ...

  8. 使用Application_Error捕获站点错误并写日志

    Global.ascx页面使用以下方法即可捕获应用层没有try cath的错误 protected void Application_Error(Object sender, EventArgs e) ...

  9. BestCoder Round 59 (HDOJ 5500) Reorder the Books

    Problem Description dxy has a collection of a series of books called “The Stories of SDOI”,There are ...

  10. MySQL忘记root密码的解决方案

    在实际操作中忘记MySQL的root密码是一件令人很头痛的事情,不要急以下的文章就是介绍MySQL的root密码忘记的时候解决方案,我们可以对其进行如下的步骤重新设置,以下就是文章的详细内容描述.   ...