双向环形链表

该文章描述了四个用于环形链表管理的函数:list_queue_printf用于打印链表所有节点数据,list_queue_indexof获取指定索引的节点,list_queue_remove_node删除指定索引的节点,而list_queue_add_node在链表头部添加新节点并保持排序。文章还提供了环形链表结构体定义和相关函数的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 四个操作接口

/**
* 遍历链表,每个节点的数据打印
*/
void list_queue_printf(list_queue_t *p_list_head);

/**
* 索引头部偏移的节点,返回指定节点的指针
*/
list_queue_t *list_queue_indexof(list_queue_t *p_list_head, uint16_t node_index);
/**
* 删除指定索引的节点
*/
list_queue_t* list_queue_remove_node(list_queue_t *p_list_head, uint16_t node_index);

/**
* 添加节点,新节点追加到头部,按照添加顺序排序
*/
list_queue_t *list_queue_add_node(list_queue_t *p_list_head, uint8_t *p_data, uint16_t length);

2. 环形链表结构体

typedef struct list_node
{
	struct list_node* p_next_node ;
	struct list_node* p_prev_node ;
    uint16_t data_length;
	uint8_t p_data[0];
}list_queue_t;

3. 环形链表的接口实现


list_queue_t *list_queue_add_node(list_queue_t *p_list_head, uint8_t *p_data, uint16_t length)
{
    if (p_data == NULL)
    {
        return p_list_head;
    }

    if (p_list_head == NULL)
    {
        p_list_head = os_mem_alloc(RAM_TYPE, sizeof(list_queue_t) + length);
        if (p_list_head == NULL)
        {
            return NULL;
        }

        p_list_head->data_length = length;
        memcpy(p_list_head->p_data, p_data, length);

        p_list_head->p_next_node = p_list_head;
        p_list_head->p_prev_node = p_list_head;

        // p_list_head = 
    }
    else
    {
        list_queue_t *p_node = os_mem_alloc(RAM_TYPE, sizeof(list_queue_t) + length);
        if (p_node == NULL)
        {
            return p_list_head;
        }

        p_node->data_length = length;
        memcpy(p_node->p_data, p_data, length);

        p_node->p_prev_node = p_list_head->p_prev_node;
        p_node->p_next_node = p_list_head;

        p_node->p_prev_node->p_next_node = p_node;
        p_node->p_next_node->p_prev_node = p_node;

        p_list_head = p_node;
    }

    return p_list_head;
}

list_queue_t *list_queue_remove_node(list_queue_t *p_list_head, uint16_t node_index)
{
    if (p_list_head == NULL)
    {
        return NULL;
    }

    list_queue_t *p_target_node = NULL;
    if (node_index == 0)
    {
        if (p_list_head->p_next_node == p_list_head)
        {
            os_mem_free(p_list_head);
            p_list_head = NULL;
            return NULL;
        }
        else
        {
            p_target_node = p_list_head;
            p_list_head = p_list_head->p_next_node;

            p_target_node->p_next_node->p_prev_node = p_target_node->p_prev_node;
            p_target_node->p_prev_node->p_next_node = p_target_node->p_next_node;

            os_mem_free(p_target_node);
            p_target_node = NULL;
            return p_list_head;
        }
    }

    uint16_t index = 0;
    for (list_queue_t *p_iterator = p_list_head;; p_iterator = p_iterator->p_next_node)
    {
        if (index == node_index)
        {
            p_target_node = p_iterator;
            break;
        }

        if (p_iterator->p_next_node == p_list_head)
        {
            return p_list_head;
        }

        index++;
    }

    if (p_target_node != NULL)
    {
        p_target_node->p_next_node->p_prev_node = p_target_node->p_prev_node;
        p_target_node->p_prev_node->p_next_node = p_target_node->p_next_node;

        os_mem_free(p_target_node);
        p_target_node = NULL;
    }
    return p_list_head;
}

list_queue_t *list_queue_indexof(list_queue_t *p_list_head, uint16_t node_index)
{

    if (p_list_head == NULL)
        return NULL;

    uint16_t index = 0;
    list_queue_t *p_iterator = NULL;
    for (p_iterator = p_list_head;; p_iterator = p_iterator->p_next_node)
    {
        if (index == node_index)
        {
            return p_iterator;
        }

        if (p_iterator->p_next_node == p_list_head)
        {
            return NULL;
        }
        index++;
    }

    return NULL;
}

void list_queue_printf(list_queue_t *p_list_head)
{
    if (p_list_head == NULL)
    {
        APP_PRINT_INFO0("[list] queue is null!");
        return;
    }

    uint16_t index = 0;
    for (list_queue_t *p_iterator = p_list_head;; p_iterator = p_iterator->p_next_node)
    {
        APP_PRINT_INFO2("[list] queue index %d, data %b", index, TRACE_BINARY(p_iterator->data_length, p_iterator->p_data));
        if (p_iterator->p_next_node == p_list_head)
        {
            return;
        }
        index++;
    }
}

4. 使用方法

list_queue_t * g_head_list= NULL;

uint8_t * str0 = "12345678!";
uint8_t * str1 = "abcdefghi!";
g_head_list = list_queue_add_node(p_head_list, str0, strlen(str0));
g_head_list = list_queue_add_node(p_head_list, str1, strlen(str1));

list_queue_printf(p_head_list);

结束!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值