硬汉嵌入式论坛

 找回密码
 立即注册
查看: 388|回复: 1
收起左侧

[有问必答] 这个链表是正常的吗?

[复制链接]

23

主题

58

回帖

127

积分

初级会员

积分
127
发表于 2023-5-24 17:17:19 | 显示全部楼层 |阅读模式



[C] 纯文本查看 复制代码
#include "list_menu.h"
Node_T *head, *son,*bro,*temp,*dirnow;

Node_T *creat_father(uint8_t *str,void (*f)(void))
{
    Node_T *temp = NULL;
    temp = (Node_T *)malloc(sizeof(Node_T));
    if (temp == NULL)
    {
        return NULL;
    }
    // temp->father = temp;
    temp->father = NULL;
    temp->brother_up = NULL;
    temp->brother_dw = NULL;
    temp->son = NULL;
    temp->str = str;
    temp->f=f;
//    printf("creat_father succeed\r\n");
//    printf("%s\t\t%x\r\n", temp->str, temp);
    return temp;
}
Node_T *creat_brother(Node_T *brother, uint8_t *str,void (*f)(void))
{
    Node_T *temp = NULL;
    Node_T *temp_1 = brother;
    temp = (Node_T *)malloc(sizeof(Node_T));
    if (temp == NULL)
    {
        return NULL;
    }
    temp->father = temp_1->father;
    while (temp_1->brother_dw != NULL)
    {
        temp_1 = temp_1->brother_dw;
    }
    temp_1->brother_dw = temp;
    temp->brother_up = temp_1;
    temp->brother_dw = NULL;
    temp->son = NULL;
    temp->str = str;
    temp->f=f;
//    printf("creat_brother succeed\r\n");
//    printf("%s\t\t%x\r\n", temp->str, temp);
    return temp;
}

Node_T *creat_son(Node_T *father, uint8_t *str,void (*f)(void))
{
    Node_T *temp = NULL;
    Node_T *temp_1 = father;
    temp = (Node_T *)malloc(sizeof(Node_T));
    if (temp == NULL)
    {
        return NULL;
    }
    temp->father = temp_1;
    temp_1->son = temp;
    temp->son = NULL;
    temp->brother_dw = NULL;
    temp->brother_up = NULL;
    temp->str = str;
    temp->f=f;
//    printf("creat_son succeed\r\n");
//    printf("%s\t\t%x\r\n", temp->str, temp);
    return temp;
}
void list_menu_int(void)
{
	head=creat_father("menu",NULL);
	son=creat_son(head,"item-1",NULL);
	bro=creat_brother(son,"item-2",NULL);
	creat_brother(bro,"item-3",NULL);
	creat_son(son,"item1-1-1",NULL);
}

[C] 纯文本查看 复制代码
typedef struct Node
{
    struct Node *father;
    struct Node *brother_up;
    struct Node *brother_dw;
    struct Node *son;
    char *str;
    void (*f)(void);
} Node_T;


按上述进行初始化后,在MDK变量观察到每个节点的结构都像第一张图一样,有多个成员。
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106630
QQ
发表于 2023-5-28 10:17:12 | 显示全部楼层
帮顶。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-4-27 08:35 , Processed in 0.211229 second(s), 26 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表