list_for_each_entry

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* 模拟 Linux 内核的 list_head 结构 */
struct list_head {
    struct list_head *next, *prev;
};

/* 模拟 container_of 宏 */
#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_HEAD_INIT(name) { &(name), &(name) }

/* 链表操作函数 */
void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}

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;
}

void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}

void list_del(struct list_head *entry)
{
    entry->next->prev = entry->prev;
    entry->prev->next = entry->next;
    entry->next = entry;
    entry->prev = entry;
}

/* 遍历宏 */
#define list_entry(ptr, type, member) container_of(ptr, type, member)

// 遍历链表的宏
#define list_for_each_entry(pos, head, member)                          \
    for (pos = list_entry((head)->next, typeof(*pos), member);           \
         &pos->member != (head);                                        \
         pos = list_entry(pos->member.next, typeof(*pos), member))

/* 前向声明 */
struct motor_control;

/* 只使用 list_head 的函数可以提前定义 */
void print_list_status(const char *list_name, struct list_head *head)
{
    printf("   [%s] 链表状态: head=[%p] head->next=[%p] head->prev=[%p]\n", 
           list_name, head, head->next, head->prev);
}

/* 声明但不定义使用 motor_control 成员的函数 */
void print_node_status(const char *node_name, struct motor_control *node);
void print_linked_list(const char *list_name, struct list_head *head);

/* 我们的业务数据结构 - 电机控制 */
struct motor_control {
    int id;                 // 电机ID
    char name[20];          // 电机名称
    int speed;              // 转速
    int direction;          // 方向
    
    struct list_head list;  // 链表节点 - "绳子"就焊在这里!
};

/* 现在可以定义所有使用 motor_control 成员的函数 */
void print_node_status(const char *node_name, struct motor_control *node)
{
    printf("   [%s] 节点状态: addr=[%p] id=%d name=%s list=[%p] list.next=[%p] list.prev=[%p]\n",
           node_name, node, node->id, node->name, &node->list, node->list.next, node->list.prev);
}

void print_linked_list(const char *list_name, struct list_head *head)
{
    printf("   [%s] 链表遍历: ", list_name);
    struct motor_control *pos;
    int count = 0;
    
    list_for_each_entry(pos, head, list) {
        printf("%s(ID:%d)[%p]", pos->name, pos->id, pos);
        count++;
        if (&pos->list != head->prev) {
            printf(" -> ");
        }
    }
    if (count == 0) {
        printf("空链表");
    }
    printf("\n");
}

int main(void)
{

    struct list_head robot_motors;
    
    /* 初始化链表头 */
    INIT_LIST_HEAD(&robot_motors);
    
    printf("=== 侵入式链表演示:机器人电机控制系统(含内存地址信息)===\n\n");
    
    printf("【初始状态】\n");
    print_list_status("robot_motors", &robot_motors);
    printf("   空链表:head->next = head->prev = head (%p)\n\n", &robot_motors);

    /* 静态分配电机对象并初始化它们的链表节点 */
    struct motor_control motor1 = {1, "Left-Wheel", 1500, 1, {NULL, NULL}};
    struct motor_control motor2 = {2, "Right-Wheel", 1480, 1, {NULL, NULL}};
    struct motor_control motor3 = {3, "Arm-Joint", 800, -1, {NULL, NULL}};
    
    /* 初始化每个电机的链表节点 */
    INIT_LIST_HEAD(&motor1.list);
    INIT_LIST_HEAD(&motor2.list);
    INIT_LIST_HEAD(&motor3.list);
    
    printf("【电机对象内存布局】\n");
    print_node_status("motor1", &motor1);
    print_node_status("motor2", &motor2);
    print_node_status("motor3", &motor3);
    printf("\n");

    /* 将电机"挂"到链表上 */
    printf("1. 组装电机到链表:\n");
    
    printf("   1.1 添加 motor1:\n");
    printf("       添加前 robot_motors: head=[%p] next=[%p] prev=[%p]\n", 
           &robot_motors, robot_motors.next, robot_motors.prev);
    list_add(&motor1.list, &robot_motors);
    printf("       添加后 robot_motors: head=[%p] next=[%p](motor1) prev=[%p](motor1)\n", 
           &robot_motors, robot_motors.next, robot_motors.prev);
    print_node_status("motor1", &motor1);
    print_linked_list("robot_motors", &robot_motors);
    printf("       链表结构: head[%p] ? motor1[%p]\n\n", &robot_motors, &motor1);
    
    printf("   1.2 添加 motor2 (头插法):\n");
    printf("       添加前 robot_motors: head->next=[%p](motor1) head->prev=[%p](motor1)\n", 
           robot_motors.next, robot_motors.prev);
    list_add(&motor2.list, &robot_motors);
    printf("       添加后 robot_motors: head->next=[%p](motor2) head->prev=[%p](motor1)\n", 
           robot_motors.next, robot_motors.prev);
    print_node_status("motor1", &motor1);
    print_node_status("motor2", &motor2);
    print_linked_list("robot_motors", &robot_motors);
    printf("       链表结构: head[%p] ? motor2[%p] ? motor1[%p]\n\n", 
           &robot_motors, &motor2, &motor1);
    
    printf("   1.3 添加 motor3 (头插法):\n");
    printf("       添加前 robot_motors: head->next=[%p](motor2) head->prev=[%p](motor1)\n", 
           robot_motors.next, robot_motors.prev);
    list_add(&motor3.list, &robot_motors);
    printf("       添加后 robot_motors: head->next=[%p](motor3) head->prev=[%p](motor1)\n", 
           robot_motors.next, robot_motors.prev);
    print_node_status("motor1", &motor1);
    print_node_status("motor2", &motor2);
    print_node_status("motor3", &motor3);
    print_linked_list("robot_motors", &robot_motors);
    printf("       链表结构: head[%p] ? motor3[%p] ? motor2[%p] ? motor1[%p]\n\n", 
           &robot_motors, &motor3, &motor2, &motor1);

    printf("2. 完整遍历所有电机:\n");
    struct motor_control *motor;
    int index = 1;
    list_for_each_entry(motor, &robot_motors, list) {
        printf("   电机%d: %s, 转速=%d RPM, 方向=%s, 结构体地址=[%p], list地址=[%p]\n", 
               index++, motor->name, motor->speed,
               motor->direction > 0 ? "正转" : "反转", motor, &motor->list);
    }
    

    list_for_each_entry(motor, &robot_motors, list)
    {
    if(strcmp(motor->name, "Right-Wheel") == 0) 
        {
            printf("   找到目标电机: %s (ID:%d, 地址:[%p])\n", motor->name, motor->id, motor);
            break;
        }
                
    }

    
    return 0;
}

另一个类似程序的 结构。

cpp 复制代码
    List_HeadAdd(&Test1.list, &Head);
    List_HeadAdd(&Test2.list, &Head);
    List_HeadAdd(&Test3.list, &Head);
    List_HeadAdd(&Test4.list, &Head);

执行后,打印结构如下:

cpp 复制代码
******Head = [00000048af1ffe40],Head->next=[00000048af1ffdc8],Head->Prev=[00000048af1ffe28]
No=0,Data=4,结构体地址=[00000048af1ffdc0], list地址=[00000048af1ffdc8], prev=[00000048af1ffe40],next=[00000048af1ffde8]
No=1,Data=3,结构体地址=[00000048af1ffde0], list地址=[00000048af1ffde8], prev=[00000048af1ffdc8],next=[00000048af1ffe08]
No=2,Data=2,结构体地址=[00000048af1ffe00], list地址=[00000048af1ffe08], prev=[00000048af1ffde8],next=[00000048af1ffe28]
No=3,Data=1,结构体地址=[00000048af1ffe20], list地址=[00000048af1ffe28], prev=[00000048af1ffe08],next=[00000048af1ffe40]
相关推荐
JiMoKuangXiangQu4 小时前
ARM64 进程虚拟地址空间布局
linux·arm64 虚拟地址布局
智码未来学堂4 小时前
探秘 C 语言算法之枚举:解锁解题新思路
c语言·数据结构·算法
阳光九叶草LXGZXJ4 小时前
达梦数据库-学习-47-DmDrs控制台命令(LSN、启停、装载)
linux·运维·数据库·sql·学习
春日见4 小时前
如何避免代码冲突,拉取分支
linux·人工智能·算法·机器学习·自动驾驶
无垠的广袤5 小时前
【VisionFive 2 Lite 单板计算机】边缘AI视觉应用部署:缺陷检测
linux·人工智能·python·opencv·开发板
阿波罗尼亚5 小时前
Kubectl 命令记录
linux·运维·服务器
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.5 小时前
Keepalived单播模式配置与实战指南
linux·服务器·负载均衡
青桔柠薯片5 小时前
数据结构:顺序表与链表
数据结构·链表
IDC02_FEIYA5 小时前
Linux文件搜索命令有哪些?Linux常用命令之文件搜索命令find详解
linux·运维·服务器
江畔何人初6 小时前
kubectl apply与kubectl create的区别
linux·运维·云原生