最近有看一点Linux内核源码,发现内核里大量使用了list_head结构体。百度查了一下,原来内核利用这个结构体实现了泛型。

  自认为对链表已经很熟悉的我,决定自己实现一下。

  下面以Node和list_head为例。

  上图就是循环链大致思路了。(画的不好)

  我们通过list_head对链表进行移动操作。

  这里存在几个问题:

    首先通过list_head得到的指针,它指向的list_head的结构体,但我们其实想要使用的是Node结构体。

    再有,我们要设计一个泛型的链表,那么,我就不可以在实现链表时有任何对Node的操作。

  解决办法:

    1、通过计算,找到node结构体的首地址。(我们通过一个宏来实现)

 #define list_entry(ptr, type, member) \
((type *)((char *)(ptr) - (char *)(&(((type *))->member))))

    这个宏看起来可能点乱,但我们把思路缕清就不乱了。

我们只知道entry的指针,如何求出Node的指针呢?

如果我们可以知道entry的相对位置,那么

  Node的实际位置  = entry的实际位置 - entry的相对位置。

  entry的相对位置 =  (&(((Node *)0)->entry))   又蒙了么?  这里,我们先将 0转换为(Node *)类型的指针,再用这个指针指向entry,最后取它的地址。这时我们就得到了entry的相对位置。

  宏中把他们强转成char * 是为了进行减法。最后再强转成Node类型,这时我就得到了Node的指针。

2、让用户自己定义特定数据的操作,我们只提供一个函数接口(我们通过函数指针来实现)

  在我们的链表里,只涉及到了list_head 里的内容,所以,不能对Node进行操作。参数也都是list_head的指针。这就需要用户在使用时,通过上面的宏来完成从list_head 到 Node的转换。在稍后例子中会了解到。

源码

dclist_head.h

 /*************************************************************************
> File Name: dlist_head.h
> Author: gaozy
> Mail: 44523253@qq.com
> Created Time: 2016年12月24日 星期六 10时11分22秒
************************************************************************/ /*
*这是我自己实现的一个泛型循环链表
*使用者只需要把dclist_head_t 这个结构体加入到自己的结构体中就可以使用这个链表了。
*在使用时,需要使用者创建一个头节点,之后使用init函数初始化。(当然,你也可以自己对他进行初始化操作)
*它很重要,否则无法使用这个链表。
*链表给使用者提供了四个函数
*init 刚刚已经说过了,我们用它初始化链表
*append 这是一个向链表中添加节点的函数,需要我们传入链表的头和新节点的dclist_head_t部分的指针
*treaverse 正如火函数名一样,它的作用是遍历链表,它需要我们提供一个函数指针
*这个指针的作用是对节点进行操作,参数是一个dclist_head_t 类型的指针,我们同过list_entry这个宏获取
*使用者自定义的数据类型。
*dc_remove这个函数用来释放链表,类似treaverse函数,需要我们自己实现删除j节点函数。
*它会释放链表中的所有节点,只有头结点例外,头需要我们自己释放
*/ #ifndef DLIST_HEAD
#define DLIST_HEAD #define list_entry(ptr, type, member) \
((type *)((char *)(ptr) - (char *)(&(((type *))->member)))) typedef struct dclist_head {
struct dclist_head * prev;
struct dclist_head * next;
} dclist_head_t; void init(dclist_head_t *head);
void append(dclist_head_t *head, dclist_head_t *node);
void treaverse(dclist_head_t *head, void (*pfun)(dclist_head_t *node) );
void dc_remove(dclist_head_t *head, void (*pfun)(dclist_head_t *node) ); #endif

dclist_head.c

 /*************************************************************************
> File Name: dclist_head.c
> Author: gaozy
> Mail: 44523253@qq.com
> Created Time: 2016年12月24日 星期六 10时19分49秒
************************************************************************/ #include <stdio.h>
#include <stdlib.h> #include "dclist_head.h" void init(dclist_head_t *head)
{
if ( head != NULL ) {
head->prev = NULL;
head->next = NULL;
}
} void append(dclist_head_t *head, dclist_head_t *node)
{
if ( head == NULL ) {
printf("error\n");
exit();
} if ( head->prev == NULL && head->next == NULL ) {
head->prev = node;
head->next = node;
node->prev = head;
node->next = head;
} else {
dclist_head_t *tmp = head->prev;
tmp->next = node;
node->prev = tmp;
node->next = head;
head->prev = node;
}
} void treaverse(dclist_head_t *head, void (*pfun)(dclist_head_t *node) )
{
if ( head == NULL || head->next == NULL )
return; dclist_head_t *tmp = head->next;
while ( tmp != head ) {
pfun(tmp);
tmp = tmp->next;
}
} void dc_remove(dclist_head_t *head, void (*pfun)(dclist_head_t *node) )
{
treaverse(head, pfun);
}

测试代码

main.c

 /*************************************************************************
> File Name: main.c
> Author: gaozy
> Mail: 44523253@qq.com
> Created Time: 2016年12月24日 星期六 11时11分25秒
************************************************************************/ #include <stdio.h>
#include <stdlib.h> #include "dclist_head.h" typedef struct {
int id;
dclist_head_t entry;
} student_t; void print(dclist_head_t *ptr)
{
student_t *stu = list_entry(ptr, student_t, entry);
if ( stu == NULL )
return;
printf("student id = %d\n", stu->id);
} void free_node(dclist_head_t *ptr)
{
if (ptr == NULL )
return; student_t *node = list_entry(ptr, student_t, entry);
free(node);
} student_t* make_node(int id)
{
student_t *stu = (student_t *)malloc(sizeof(student_t));
if ( stu != NULL ) {
stu->id = id;
} return stu;
} int main(void)
{
dclist_head_t list; init(&list); int i;
student_t *stu;
for ( i=; i<; i++ ) {
stu = make_node(i);
if ( stu != NULL )
append(&list, &stu->entry);
} treaverse(&list, print);
dc_remove(&list, free_node); return ;
}

水平有限,还请大神多多指点。

c语言是如何实现泛型链表的更多相关文章

  1. 数据结构C语言版 有向图的十字链表存储表示和实现

    /*1wangxiaobo@163.com 数据结构C语言版 有向图的十字链表存储表示和实现 P165 编译环境:Dev-C++ 4.9.9.2 */ #include <stdio.h> ...

  2. 数据结构(c语言第2版)-----了解链表,栈,队列,串

    关于链表我觉得这都是最基本的东西,但是不常见,在实际的应用中很少的使用,了解它会用就OK,不需要研究的那么深,除非做那种内存压缩,存储方面工作. C语言中动态申请空间 malloc() q=(dlin ...

  3. 深入浅出数据结构C语言版(5)——链表的操作

    上一次我们从什么是表一直讲到了链表该怎么实现的想法上:http://www.cnblogs.com/mm93/p/6574912.html 而这一次我们就要实现所说的承诺,即实现链表应有的操作(至于游 ...

  4. C语言 复杂的栈(链表栈)

    //复杂的栈--链表栈 #include<stdio.h> #include<stdlib.h> #define datatype int//定义链表栈数据类型 //定义链表栈 ...

  5. Java基础之泛型——使用泛型链表类型(TryGenericLinkedList)

    控制台程序 定义Point类: public class Point { // Create a point from its coordinates public Point(double xVal ...

  6. 基于visual Studio2013解决C语言竞赛题之0808打印链表

     题目

  7. c语言实现两个单链表的交叉合并

    #include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; struc ...

  8. C++学习(三十五)(C语言部分)之 单链表

    单链表 就好比火车 火车头-->链表头部火车尾-->链表尾部火车厢-->链表的节点火车厢连接的部分-->指针火车中的内容-->链表节点的的数据链表节点包含数据域和指针域数 ...

  9. C语言版本:循环单链表的实现

    SClist.h #ifndef __SCLIST_H__ #define __SCLIST_H__ #include<cstdio> #include<malloc.h> # ...

随机推荐

  1. 说说设计模式~装饰器模式(Decorator)

    返回目录 装饰器模式,也叫又叫装饰者模式,顾名思义,将一个对象进行包裹,包装,让它变成一个比较满意的对象,这种模式在我们平时项目开发中,经常会用到,事实上,它是处理问题的一种技巧,也很好的扩展了程序, ...

  2. Atitit.java图片图像处理attilax总结

    Atitit.java图片图像处理attilax总结 BufferedImage extends java.awt.Image 获取图像像素点 image.getRGB(i, lineIndex); ...

  3. c#设计模式-观察者模式

    Observer 与 Subject 互为耦合,但是这种耦合的双方都依赖于抽象,而不依赖于具体. 一.观察者模式 目的 我们都知道解决一个问题有N种解决方式,但在面向对象的设计中如何能做到“高内聚,低 ...

  4. js 事件

    事件:一般用于浏览器与用户操作进行交互 js事件的三种模型:内联模型.脚本模型.DOM2模型 内联模型:事件处理函数是HTML标签的属性 <input type="button&quo ...

  5. transform:rotate()将元素进行不同角度的旋转

    通过设置transform:rotate()可以将元素进行不同角度的旋转: 下面是一些测试代码: <!DOCTYPE html> <html> <head> < ...

  6. SQLServer清空数据库中所有的表并且ID自动归0

    exec sp_MSforeachtable 'Truncate Table ?'

  7. Node学习

    参见Node入门 做出node应用的第一个例子 图片上传浏览.

  8. Linux - Screen

    GNU's Screen homepage Screen是由GNU计划开发的用于命令行终端切换的自由软件,可以看作是窗口管理器的命令行界面版本. 可以通过该软件同时连接多个本地或远程的命令行会话,并在 ...

  9. MAC与HMAC的介绍及其在AWS和Azure中的应用

    MAC 在密码学中,(消息认证码)Message Authentication Code是用来认证消息的比较短的信息.换言之,MAC用来保证消息的数据完整性和消息的数据源认证. MAC由消息本身和一个 ...

  10. 第19/24周 锁升级(Lock Escalations)

    大家好,欢迎回到性能调优培训.上2个星期我们已经讨论了SQLServer里的悲观和乐观锁.今天我想谈下SQL Server里对于锁的一个特殊现象:所谓的锁升级(Lock Escalations).在我 ...