C 里面如何使用链表 list

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

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

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

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

复制代码
#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;

或者类似

复制代码
/**
 * 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岁中年之后

复制代码
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 指针来回调整.

相关推荐
为何创造硅基生物8 小时前
C语言 结构体内存对齐规则(通俗易懂版)
c语言·开发语言
仰泳之鹅9 小时前
【C语言】自定义数据类型2——联合体与枚举
c语言·开发语言·算法
jolimark9 小时前
C语言自学攻略:小白入门三步走
c语言·编程入门·学习路线·实践项目·自学攻略
cen__y10 小时前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git
社交怪人11 小时前
【算平均分】信息学奥赛一本通C语言解法(题号2071)
c语言·开发语言
卢锡荣12 小时前
单芯通吃,盲插标杆 —— 乐得瑞 LDR6020,Type‑C 全场景互联 “智慧芯”
c语言·开发语言·计算机外设
AI科技星12 小时前
《数学公理体系·第三部·数术几何》(2026 年版)
c语言·开发语言·线性代数·算法·矩阵·量子计算·agi
kkeeper~13 小时前
0基础C语言积跬步之字符函数与字符串函数(上)
c语言·开发语言
m0_6294947313 小时前
LeetCode 热题 100-----25.回文链表
数据结构·算法·leetcode·链表
東隅已逝,桑榆非晚14 小时前
字符函数和字符串函数
c语言·笔记