基于littleVGL的双动态链表分析

一、 数据类型

该分析模型为littleVGL内的链表模块,主要为文件lv_ll.hlv_ll.c
在这里插入图片描述

  • 链表定义

每个链表的通过结构体lv_ll_t 进行定义,改结构体包含每个节点大小的定义、头部指针、尾部指针。

/** Description of a linked list*/
typedef struct {
    uint32_t n_size;
    lv_ll_node_t * head;
    lv_ll_node_t * tail;
} lv_ll_t;
  • 节点的定义
    节点的定义根据实际使用,在此,假设节点的数据类型为Student_t,包含2个成员。
typedef struct
{
    uint8_t name[6];
    uint32_t age;
}Student_t;
  • 链表的初始化
    链表初始化函数的定义如下,对链表的节点大小、头部、尾部进行赋值。
void _lv_ll_init(lv_ll_t * ll_p, uint32_t node_size)
{
    ll_p->head = NULL;
    ll_p->tail = NULL;
    node_size = (node_size + 3) & (~0x3);//4字节对齐
    ll_p->n_size = node_size;
}

实例引用如下
定义链表类型变量为ll_test,通过_lv_ll_init进行初始化

lv_ll_t ll_test;
_lv_ll_init(&ll_test, sizeof(Student_t));
  • 插入节点
    节点的插入从链表的头开始插入,即插入到第一个节点的前面。
    实现步骤为
    a)新节点n_new为新的头,其前面没有节点,所以该节点的头部指针指向NULL
    在这里插入图片描述
    b)将新节点n_new的尾部指向原来的头ll_p->head
    在这里插入图片描述
    c)将原来的头部的节点的头指针指向新接点
    在这里插入图片描述
    d)更新链表的头部指针信息

实现源码

void * _lv_ll_ins_head(lv_ll_t * ll_p)
{
    lv_ll_node_t * n_new;

    n_new = lv_mem_alloc(ll_p->n_size + LL_NODE_META_SIZE);//为新节点申请空间

    if(n_new != NULL) {
        // 将新节点的头指向NULL
        node_set_prev(ll_p, n_new, NULL);       /*No prev. before the new head*/
        //将新节点n_new的尾部指向原来的头ll_p->head
        node_set_next(ll_p, n_new, ll_p->head); /*After new comes the old head*/

        if(ll_p->head != NULL) { /*If there is old head then before it goes the new*/
            //将原来的头部的节点的头指针指向新接点
            node_set_prev(ll_p, ll_p->head, n_new);
        }
        //更新原来的链表头部指针信息
        ll_p->head = n_new;      /*Set the new head in the dsc.*/
        if(ll_p->tail == NULL) { /*If there is no tail (1. node) set the tail too*/
            ll_p->tail = n_new;
        }
    }
    return n_new;
}

实例引用:

    Student_t *tempact;

    tempact = _lv_ll_ins_head(&ll_test);
    memcpy(tempact->name,"12345",6);
    tempact->age=29;

二、完整实例

typedef struct
{
    uint8_t name[6];
    uint32_t age;
}Student_t;


int main()
{
    lv_ll_t ll_test;
    Student_t nanty;
    Student_t *tempact;
    uint32_t node_len=0;

    _lv_ll_init(&ll_test, sizeof(Student_t));
    lv_ll_node_t *act;


    tempact = _lv_ll_ins_head(&ll_test);
    memcpy(tempact->name,"12345",6);
    tempact->age=29;

    tempact = _lv_ll_ins_head(&ll_test);
    memcpy(tempact->name,"nanty",6);
    tempact->age=28;

    node_len = _lv_ll_get_len(&ll_test);
    printf("node_len=%d\n",node_len);



    tempact = _lv_ll_get_head(&ll_test);
    printf("name=%s,age=%d\n",tempact->name,tempact->age);

    tempact = _lv_ll_get_next(&ll_test, tempact);
    printf("name=%s,age=%d\n",tempact->name,tempact->age);

    while(1);
    return 0;
}

运行结果:
在这里插入图片描述

附:完整测试代码

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

loveshipting

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值