一、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 *)0)->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 = 1;stu1.english = 0;stu1.math = 0;list_add_tail(&(stu1.list), &(score_head));stu2.num = 2;stu2.english = 1;stu2.math = 1;list_add_tail(&(stu2.list), &(score__head));stu3.num = 3;stu3.english = 2;stu3.math = 2;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 0;
}

三、运行结果

转自:https://www.cnblogs.com/51qianrushi/p/4294406.html

Linux内核链表的移植与使用相关推荐

  1. linux内核链表移植到windows平台

    说起linux内核链表, 看到的人都会惊讶,链表竟然可以写如此优雅通用.简直是对现实中链条的形象模拟.链条可以串联上各种各种各样的,奇形怪状的的东西,只要每个东西有能够穿过链条的孔洞,linux内核链 ...

  2. Linux 内核链表剖析(二十)

    上节博客中,我们讲到了 Linux 中的宏定义 offsetof 与 container_of 宏.那么本节我们的课程目标就是一直 Linux 内核链表,使其适用于非 GNU 编译器,分析 Linux ...

  3. linux内核链表使用例,linux内核链表的使用例子

    linux内核链表的使用例子 #include #include #include #include #include #include MODULE_LICENSE("GPL") ...

  4. linux内核链表以及list_entry--linux内核数据结构(一)

    传统的链表实现 之前我们前面提到的链表都是在我们原数据结构的基础上增加指针域next(或者prev),从而使各个节点能否链接在一起, 比如如下的结构信息 typedef struct fox { un ...

  5. linux内核链表分析

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

  6. Linux内核链表交换节点,[笔记]Linux内核链表:结点的插入、删除以及链表的遍历...

    Linux内核链表:结点的插入.删除以及链表的遍历 1. Linux内核链表的核心思想是:在用户自定义的结构A中声明list_head类型的成员p,这样每个结构类型为A的变量a中,都拥有同样的成员p, ...

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

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

  8. 第六天2017/04/11(2:Linux内核链表Demo、顺序表、链表的开发与设计)

    //结构体相关的高级话题#include "stdio.h" #include "stdlib.h" #include "string.h" ...

  9. linux内核链表的使用

    linux内核链表: 链表通常包括两个域:数据域和指针域. struct list_head{ struct list_head *next,*prev; }; include/linux/list. ...

最新文章

  1. 系统监控:top vs Htop vs Glances
  2. Docker源码编译(二)
  3. python读取txt为dataframe_python批量读取txt文件为DataFrame的方法
  4. 【机器学习应用】将在2020年实现盈利的12个基于AI和ML的最佳应用创意
  5. SolrCloud6.1.0之SQL查询测试
  6. Python中collections模块
  7. Web控件TreeView展开无闪烁的两个解决方法
  8. 跨模块中的分配内存问题
  9. webpack打包vue2.0项目时必现问题
  10. 设置橘子浏览器的newtab页面
  11. 基于 vue + zhengAdmin 的一套后台模板
  12. react中创建组件的三种方法
  13. CS0016错误解决汇编
  14. css 鼠标呈现手指型
  15. acr122 java,ACR122U中文开发文档
  16. c语言摇奖小程序,小程序实现抽奖动画
  17. html5吻胸小游戏,html5气球大战小游戏代码
  18. C++ Reference: Standard C++ Library reference: C Library: cstdlib: atoll
  19. 【197期】华为OD两轮技术面试记录,给后来人一个参考!
  20. 【牛奶豆腐汤喝出小蛮腰】

热门文章

  1. HashTable 和HashMap区别
  2. 自然语言处理中的模式(模式0:模式无处不在模式)
  3. 【终极办法】windows下安装完MySQL,为什么cmd不识别命令?
  4. (*长期更新)软考网络工程师学习笔记——Section 10 网络安全
  5. php 虚基类,1.9 多态
  6. 获取昨天凌晨毫秒数_Java 获取当前时间距离当天凌晨的秒数
  7. python编写购物程序_Python实现购物程序思路及代码
  8. miniGUI安装笔记(转)
  9. excel随机排序,在A列产生顺序号
  10. 海南2021高考成绩分数查询时间,2021海南高考成绩查询公布时间今天几点可以查询...