C 里面如何使用链表 list

c 的世界可能高频业务都依赖 list 增删改查. 这里简单交流下自己在 c 里面使用 list

1. 学生时代, 那会学习 C 数据结构, 比较简单

复制代码
struct person {
    int id;
    char name[64+1];
    struct person * next;
};

类似上面这样, 需要什么依赖 next 指针来回调整, 然后手工 print F5 去 debug 熬.

2. 刚工作青年时代, 主要花活, 随大流

类似

structc/modular/test/list.h at master · wangzhione/structc

复制代码
#pragma once

#include "struct.h"

//
// list.h 似魔鬼的步伐, 单链表库
// $LIST 需要嵌入 struct 的第一行
// void * list = nullptr;      //        create list
// list_delete(list, fide); // [可选] delete list
//
struct $list {
    struct $list * next;
};

#define $LIST struct $list $node;

或者类似

ccan/ccan/list/list.h at master · rustyrussell/ccan

复制代码
/**
 * struct list_node - an entry in a doubly-linked list
 * @next: next entry (self if empty)
 * @prev: previous entry (self if empty)
 *
 * This is used as an entry in a linked list.
 * Example:
 *    struct child {
 *        const char *name;
 *        // Linked list of all us children.
 *        struct list_node list;
 *    };
 */
struct list_node
{
    struct list_node *next, *prev;
};

/**
 * struct list_head - the head of a doubly-linked list
 * @h: the list_head (containing next and prev pointers)
 *
 * This is used as the head of a linked list.
 * Example:
 *    struct parent {
 *        const char *name;
 *        struct list_head children;
 *        unsigned int num_children;
 *    };
 */
struct list_head
{
    struct list_node n;
};

杂技, 理解的心智负担稍微高一点, 但使用上对方有了单元测试, 比较成熟, list 结构问题较少, 除了业务的内存错位自己 debug 稍微麻烦点.

3. 35岁中年之后, 又想起刚开始那会

skynet/skynet-src/socket_server.c at master · cloudwu/skynet

类似这样

复制代码
struct write_buffer {
    struct write_buffer * next;
    const void *buffer;
    char *ptr;
    size_t sz;
    bool userobject;
};

struct write_buffer_udp {
    struct write_buffer buffer;
    uint8_t udp_address[UDP_ADDRESS_SIZE];
};

struct wb_list {
    struct write_buffer * head;
    struct write_buffer * tail;
};

需要 list , 还是直接 next 指针来回调整.

当下各种 ai 加持, 这种方式可能是最简单最直接, 当然 c 写代码相对麻烦, 多做好单元测试.

人生也类似, 兜兜转转一个圈, 那种圈在时空维度看, 是螺旋上升的.

不知道有没有人好奇, 为什么不直接一开始就上升呢, 可能生命不需要赶着投胎吧, 浪费不是时间, 也不是人生, 也可能是享受到了时间, 享受到了自己来回波动的人生.

: ) Good luckly ~

4. 未来时代, 对于个人而言, C 融入自己思维一部分, 可能不再去主动写了.

类似婴儿那会记忆, 与其说忘了, 已经存在于脑海最底层机制里面了.

相关推荐
小宇子2B3 天前
一个 7 行的 C 函数,是怎么一路变成 CPU 上的电信号
c·汇编语言
handler013 天前
【算法】并查集(普通/扩展/带权)模板与例题
数据结构·c++·笔记·算法·c·图论·查并集
蓝宝石的傻话4 天前
给MibeeNvr 0.6调试的Esp32和树莓派的三个摄像头项目的技术更新细节
c
handler016 天前
【C++11 】Lambda 表达式、std::function 与 std::bind 解析
c++·c·c++11·bind·解耦·function·lamda
handler0111 天前
【C++】二叉搜索树详解及其模拟实现(代码)
开发语言·c++·算法·c··二叉搜索树·搜索树
爱学习的程序媛12 天前
C 语言全景指南:从底层原理到工业级实战
c++·c#·c
dozenyaoyida13 天前
RISC-V嵌入式开发:彻底解决“undefined reference to isatty“错误全攻略
经验分享·c·cmake·嵌入式开发·isatty·没有定义问题
Shadow(⊙o⊙)14 天前
模拟实现:glibc_1.0-文件操作函数fopen fclose fwrite fflush实现。
开发语言·c++·学习·c
liulilittle16 天前
TCP UCP:基于卡尔曼滤波的BBR增强型拥塞控制算法
linux·网络·c++·tcp/ip·算法·c·通讯
weixin_4217252617 天前
C语言、C++与C#深度研究报告:从底层控制到现代企业级开发的演进
c语言·c++·c·内存管理·编译模型