一、
   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. Trie和Ternary Search Tree介绍

    Trie树 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie树与二叉搜索树不同,键不是直接保存在节 ...

  2. struts2,hibernate,spring整合笔记(1)

    今天终于配置好了ssh框架的整合,记录下过程供参考 环境:window8.1,jdk1.7 ,带有javaee的eclipse,也就是说要能发布web项目,TOMCAT服务器,tomcat配置涉及到环 ...

  3. [转] 深入剖析 linux GCC 4.4 的 STL string

    本文通过研究STL源码来剖析C++中标准模板块库std::string运行机理,重点研究了其中的引用计数和Copy-On-Write技术. 平台:x86_64-redhat-linux gcc ver ...

  4. Spark Streaming与kafka整合实践之WordCount

    本次实践使用kafka console作为消息的生产者,Spark Streaming作为消息的消费者,具体实践代码如下 首先启动kafka server .\bin\windows\kafka-se ...

  5. C#中的序列化与反序列化

    眼看XX鸟的课程关于C#的知识点就要学完了,翻看网络中流传的教程还是发现了一个课程中没有讲到的知识点:序列化与反序列化 无奈还是了解一下并操作一番,以备后用吧 介绍:就是将对象信息转化为二进制信息以便 ...

  6. css元素居中

    水平居中 若为行内元素,对其父元素用text-align:center即可: 若为块元素(无浮动),则一般有两种方法可实现对其的水平居中,一为margin:0 auto;二为通过css计算函数calc ...

  7. zookeeper集群的安装

    顾名思义zookeeper就是动物园管理员,他是用来管hadoop(大象).Hive(蜜蜂).pig(小猪)的管理员, Apache Hbase和 Apache Solr 的分布式集群都用到了zook ...

  8. C++ for 循环

    C++的另一种for循环写法,和C#的foreach语法很类似,不需要知道数组的类型: C++:for(auto& item:items) C#:foreach(var item in ite ...

  9. WARNING OGG-01223 TCP/IP error 111 (Connection refused)

    一:问题描述 GGSCI (source_pc) 64> info all Program     Status      Group       Lag at Chkpt  Time Sinc ...

  10. Revenge of Fibonacc

    算法:搜索: In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence rela ...