通用双向链表的设计(参考Linux系统中的实现)

简介: 通常我们设计设计链表都是将数据域放在里面,这样每次需要使用链表的时候都需要实现一个链表,然后重新实现它的相关操作,这里参考Linux系统中的设计实现了一个通用的双向链表,只需要在你的结构里面有一个这个链表的域,就可以使用链表的相关操作了。

通常我们设计设计链表都是将数据域放在里面,这样每次需要使用链表的时候都需要实现一个链表,然后重新实现它的相关操作,这里参考Linux系统中的设计实现了一个通用的双向链表,只需要在你的结构里面有一个这个链表的域,就可以使用链表的相关操作了。

注意:这个通用的双向链表是参考Linux系统中的实现,它使用了typeof这个功能,所以有些编译器可能不支持。我是再Windows系统中使用MinGW下使用GCC编译的。

////////////////////////////////////////////////////////////////////////////////////////

// list.h

 
#ifndef _list_h
#define _list_h
 
typedef struct _list_head {
    struct _list_head *prev,*next;
} list_head;
 
#define offsetof(TYPE,MEMBER) ( (size_t) &((TYPE*)0)->MEMBER )
 
#define container_of(ptr,type,member) ({\
    const typeof( ((type*)0)->member ) *__mptr = (ptr);\
    (type*)( (char*)__mptr - offsetof(type,member) );})
 
#define list_empty(head) ( head->next==0&&head->prev==0 )
 
/* get the member of list object
 * @ptr        pointer to list_head
 * @type    the type of container which contains list_head field
 * @memeber field name in the container
 * @return    return pointer to the container
 */
#define list_entry(ptr,type,member) container_of(ptr,type,member)
 
/* add a new node after `head`
 */
void list_add(list_head *head,list_head *node);
 
/* delete a node
 */
void list_del(list_head *node);
 
#endif

/////////////////////////////////////////////////////////

// list.c

#include "list.h"
 
/* add a new node after `head`
 */
void list_add(list_head *head,list_head *node) {
    if(list_empty(head)) {
        head->next = head;
        head->prev = head;
    }
    node->next = head->next;
    node->prev = head;
    head->next->prev = node;
    head->next = node;
}
/* delete a node
 */
void list_del(list_head *node) {
    node->prev->next = node->next;
    node->next->prev = node->prev;
}

///////////////////////////////////////////////////////////////////////////////

// test.c

#include <stdio.h>
#include <assert.h>
#include "list.h"
 
typedef struct _task {
    int id;
    list_head next;
} task;
 
#define task_next(t) ( container_of(t->next.next,task,next) )
 
void task_print(task *t) {
    printf("#%d -> ",t->id);
}
 
void task_foreach(task *head,void (*callback)(task *)) {
    task *p = head;
    do {
        callback(p);
        p = task_next(p);
    }
    while (p!=head);
}
 
// use task like a list
void test_list() {
    task t1={1,{0,0}},
        t2={2,{0,0}},
        t3={3,{0,0}},
        t4={4,{0,0}},
        t5={5,{0,0}};
 
    list_add(&t1.next,&t2.next);
    list_add(&t2.next,&t3.next);
    list_add(&t3.next,&t4.next);
    list_add(&t4.next,&t5.next);
 
    task_foreach(&t1,task_print);
}
 
int main(int argc, char *argv[]) {
    test_list();
 
    return 0;
}

编译运行

    gcc test.c list.h list.c -o test
    .\test.exe

下载代码

目录
相关文章
|
机器学习/深度学习 移动开发 Linux
linux通用双向链表
1, 基于linux4.9版本 #include #include #include #if 1 struct list_head { struct list_head *next, *prev; }; ...
935 0
|
Linux 机器学习/深度学习 C语言
linux内核源码“双向链表list_head”续
      上篇博文《linux内核源码“双向链表list_head”》中以一个实例介绍了list_head双向链表的用法,只有实例的代码,并没有list_head链表的代码,考虑到各位好学博友的强烈愿望,今天把list_head的代码即list.h头文件粘贴到此,供各位好学博友使用。
859 0
|
Linux
linux内核源码“双向链表list_head”
摘要:linux内核源码真是好东东,是众多高手思维的结晶,在linux 源代码中有个头文件为list.h 。很多linux 下的源代码都会使用这个头文件,它里面定义了一个结构, 以及定义了和其相关的一组函数,这个结构是这样的: struct list_head{ struct list_head *next, *prev; };         如果您之前学过双向链表,那么当你看到这个结构的时候,会觉得似曾相识。
1261 0
|
Linux C语言 C++
linux下c语言 双向链表
C/C++ code /*sgx 2008-10-30 c语言 双向链表*/#include #include #include #define TRUE 1;#define FALSE 0;typedef int ELEMTYPE;typedef struct DoubleLinkNode{...
768 0
|
1月前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
293 1
二、Linux文本处理与文件操作核心命令
|
1月前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
556 56
|
18天前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
156 2
|
28天前
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
80 0
Linux内存问题排查命令详解
|
1月前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
141 16