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]
相关推荐
LeenixP14 小时前
RK3576-Debian12删除userdata分区
linux·运维·服务器·数据库·debian·开发板
warton8814 小时前
ubuntu24 安装 proxsql 实现数据库代理
linux·运维·mysql·ubuntu
会跑的葫芦怪14 小时前
cursor 打开wsl Ubuntu项目
linux·运维·ubuntu
用户4672445449915 小时前
linux的网络配置
linux
千金裘换酒15 小时前
LeetCode 链表两数相加
算法·leetcode·链表
Lueeee.15 小时前
字符设备驱动---自己实现点LED
linux
Full Stack Developme15 小时前
linux sudo su su - 三者区别
linux·运维·服务器
Byron Loong15 小时前
【系统】Linux内核和发行版的关系
linux·运维·服务器
SmartRadio16 小时前
在CH585M代码中如何精细化配置PMU(电源管理单元)和RAM保留
linux·c语言·开发语言·人工智能·单片机·嵌入式硬件·lora