打印
[APM32F4]

【 极海APM32F4xx Tiny】学习笔记08-RTT 链表使用方法

[复制链接]
710|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
chejia12|  楼主 | 2023-7-22 09:47 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.双向链表基本结构[url=]复制[/url]
  • #define rt_inline static __inline
  • /**
  • * 双向链表
  • */
  • struct rt_list_node
  • {
  • struct rt_list_node *next; /**< point to next node. */
  • struct rt_list_node *prev; /**< point to prev node. */
  • };
  • typedef struct rt_list_node rt_list_t; /**< Type for lists. */

2.节点初始化—2个方法[url=]复制[/url]
  • /**
  • * <a  target="_blank">@brief</a> initialize a list object
  • 初始化链表,使nex= pre = 自己
  • */
  • #define RT_LIST_OBJECT_INIT(object) { &(object), &(object) }

[url=]复制[/url]
  • /**
  • * @brief initialize a list
  • *
  • * @param l list to be initialized
  • 初始化链表,使nex= pre = 自己
  • */
  • rt_inline void rt_list_init(rt_list_t *l)
  • {
  • l->next = l->prev = l;
  • }

3.在某一个节点后边插入一个节点[url=]复制[/url]
  • /**
  • * @brief insert a node after a list
  • *
  • * @param l list to insert it
  • * @param n new node to be inserted
  • 向链表某一个节点后边插入一个节点
  • */
  • rt_inline void rt_list_insert_after(rt_list_t *l, rt_list_t *n)
  • {
  • l->next->prev = n;
  • n->next = l->next;
  • l->next = n;
  • n->prev = l;
  • }

4.在某一个节点前边插入一个节点[url=]复制[/url]
  • /**
  • * @brief insert a node before a list
  • *
  • * @param n new node to be inserted
  • * @param l list to insert it
  • 在某一个节点之前插入一个节点
  • */
  • rt_inline void rt_list_insert_before(rt_list_t *l, rt_list_t *n)
  • {
  • l->prev->next = n;
  • n->prev = l->prev;
  • l->prev = n;
  • n->next = l;
  • }

5.移除一个节点[url=]复制[/url]
  • /**
  • * @brief remove node from list.
  • * @param n the node to remove from the list.
  • 移除一个节点
  • */
  • rt_inline void rt_list_remove(rt_list_t *n)
  • {
  • n->next->prev = n->prev;
  • n->prev->next = n->next;
  • n->next = n->prev = n;
  • }

6.判定链表是不是空

就是判定是不是自己指向自己

[url=]复制[/url]
  • /**
  • * @brief tests whether a list is empty
  • * @param l the list to test.
  • 判定链表是不是空链表,就是判定是不是自己指向自己
  • */
  • rt_inline int rt_list_isempty(const rt_list_t *l)
  • {
  • return l->next == l;
  • }

7.获取链表长度[url=]复制[/url]
  • /**
  • * @brief get the list length
  • * @param l the list to get.
  • 获取链表长度
  • */
  • rt_inline unsigned int rt_list_len(const rt_list_t *l)
  • {
  • unsigned int len = 0;
  • const rt_list_t *p = l;
  • while (p->next != l)
  • {
  • p = p->next;
  • len ++;
  • }
  • return len;
  • }

已知结构体内部一个成员的地址can_dev,根据机构体类型struct ra_can和成员名字can_dev 得到结构体实例对象can的地址

8.获取结构体的入口地址[url=]复制[/url]
  • /**
  • * @brief 获取结构体的入口地址get the struct for this entry
  • * @param 结构体中一个成员的入口地址 node the entry point
  • * @param 结构体的名字 type the type of structure
  • * @param 结构体内部成员的名字 member the name of list in structure
  • 已知成员的地址,获取机构体的头部
  • */
  • #define rt_list_entry(node, type, member) \
  • rt_container_of(node, type, member)

9.遍历整个链表[url=]复制[/url]
  • /**
  • * rt_list_for_each - iterate over a list
  • * @param pos the rt_list_t * 传入一个链表指针,用于返回每个成员的指针
  • * @param 传入链表的头指针 head the head for your list.
  • 链表遍历
  • */
  • #define rt_list_for_each(pos, head) \
  • for (pos = (head)->next; pos != (head); pos = pos->next)

10.已知成员的地址遍历整个链表[url=]复制[/url]
  • /**
  • * rt_list_for_each_entry - iterate over list of given type
  • * @param pos 结构体类型的指针,用于返回结构体的入口地址 the type * to use as a loop cursor.
  • * @param head 链表头指针 the head for your list.
  • * @param 接头体成语名称 member the name of the list_struct within the struct.
  • */
  • #define rt_list_for_each_entry(pos, head, member) \
  • for (pos = rt_list_entry((head)->next, typeof(*pos), member); \
  • &pos->member != (head); \
  • pos = rt_list_entry(pos->member.next, typeof(*pos), member))

我移植注释过的链表[url=]复制[/url]
  • #ifndef __RTT_LIST_H__
  • #define __RTT_LIST_H__
  • #define rt_inline
  • /**
  • * Double List structure
  • */
  • struct rt_list_node
  • {
  • struct rt_list_node *next; /**< point to next node. */
  • struct rt_list_node *prev; /**< point to prev node. */
  • };
  • typedef struct rt_list_node rt_list_t; /**< Type for lists. */
  • /**
  • * @addtogroup KernelService
  • */
  • /**@{*/
  • /**
  • * rt_container_of - return the start address of struct type, while ptr is the
  • * member of struct type.
  • 给定一个结构体成员地址,返回结构体的首地址
  • */
  • #define rt_container_of(ptr, type, member) \
  • ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
  • /**
  • * @brief initialize a list object
  • 初始化链表,使nex= pre = 自己
  • */
  • #define RT_LIST_OBJECT_INIT(object) { &(object), &(object) }
  • /**
  • * @brief initialize a list
  • *
  • * @param l list to be initialized
  • 初始化链表,使nex= pre = 自己
  • */
  • rt_inline void rt_list_init(rt_list_t *l)
  • {
  • l->next = l->prev = l;
  • }
  • /**
  • * @brief insert a node after a list
  • *
  • * @param l list to insert it
  • * @param n new node to be inserted
  • 向链表某一个节点后边插入一个节点
  • */
  • rt_inline void rt_list_insert_after(rt_list_t *l, rt_list_t *n)
  • {
  • l->next->prev = n;
  • n->next = l->next;
  • l->next = n;
  • n->prev = l;
  • }
  • /**
  • * @brief insert a node before a list
  • *
  • * @param n new node to be inserted
  • * @param l list to insert it
  • 在某一个节点之前插入一个节点
  • */
  • rt_inline void rt_list_insert_before(rt_list_t *l, rt_list_t *n)
  • {
  • l->prev->next = n;
  • n->prev = l->prev;
  • l->prev = n;
  • n->next = l;
  • }
  • /**
  • * @brief remove node from list.
  • * @param n the node to remove from the list.
  • 移除一个节点
  • */
  • rt_inline void rt_list_remove(rt_list_t *n)
  • {
  • n->next->prev = n->prev;
  • n->prev->next = n->next;
  • n->next = n->prev = n;
  • }
  • /**
  • * @brief tests whether a list is empty
  • * @param l the list to test.
  • 判定链表是不是控制,就是判定是不是自己指向自己
  • */
  • rt_inline int rt_list_isempty(const rt_list_t *l)
  • {
  • return l->next == l;
  • }
  • /**
  • * @brief get the list length
  • * @param l the list to get.
  • 获取链表长度
  • */
  • rt_inline unsigned int rt_list_len(const rt_list_t *l)
  • {
  • unsigned int len = 0;
  • const rt_list_t *p = l;
  • while (p->next != l)
  • {
  • p = p->next;
  • len ++;
  • }
  • return len;
  • }
  • /**
  • * @brief 获取结构体的入口地址get the struct for this entry
  • * @param 结构体中一个成员的入口地址 node the entry point
  • * @param 结构体的名字 type the type of structure
  • * @param 结构体内部成员的名字 member the name of list in structure
  • 已知成员的地址,获取机构体的头部
  • */
  • #define rt_list_entry(node, type, member) \
  • rt_container_of(node, type, member)
  • /**
  • * rt_list_for_each - iterate over a list
  • * @param pos the rt_list_t * to use as a loop cursor.
  • * @param head the head for your list.
  • 链表遍历
  • */
  • #define rt_list_for_each(pos, head) \
  • for (pos = (head)->next; pos != (head); pos = pos->next)
  • /**
  • * rt_list_for_each_safe - iterate over a list safe against removal of list entry
  • * @param pos the rt_list_t * to use as a loop cursor.
  • * @param n another rt_list_t * to use as temporary storage
  • * @param head the head for your list.
  • */
  • #define rt_list_for_each_safe(pos, n, head) \
  • for (pos = (head)->next, n = pos->next; pos != (head); \
  • pos = n, n = pos->next)
  • /**
  • * rt_list_for_each_entry - iterate over list of given type
  • * @param pos 结构体类型的指针,用于返回结构体的入口地址 the type * to use as a loop cursor.
  • * @param head 链表头指针 the head for your list.
  • * @param 接头体成语名称 member the name of the list_struct within the struct.
  • */
  • #define rt_list_for_each_entry(pos, head, member) \
  • for (pos = rt_list_entry((head)->next, typeof(*pos), member); \
  • &pos->member != (head); \
  • pos = rt_list_entry(pos->member.next, typeof(*pos), member))
  • /**
  • * rt_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  • * @param pos the type * to use as a loop cursor.
  • * @param n another type * to use as temporary storage
  • * @param head the head for your list.
  • * @param member the name of the list_struct within the struct.
  • */
  • #define rt_list_for_each_entry_safe(pos, n, head, member) \
  • for (pos = rt_list_entry((head)->next, typeof(*pos), member), \
  • n = rt_list_entry(pos->member.next, typeof(*pos), member); \
  • &pos->member != (head); \
  • pos = n, n = rt_list_entry(n->member.next, typeof(*n), member))
  • /**
  • * rt_list_first_entry - get the first element from a list
  • * @param ptr the list head to take the element from.
  • * @param type the type of the struct this is embedded in.
  • * @param member the name of the list_struct within the struct.
  • *
  • * Note, that list is expected to be not empty.
  • */
  • #define rt_list_first_entry(ptr, type, member) \
  • rt_list_entry((ptr)->next, type, member)
  • #endif

rtt_list 链表使用实例[url=]复制[/url]
  • #include "stdio.h"
  • #include <stdint.h>
  • #include "rtt_list.h"
  • //定义一个数据结构
  • typedef struct {
  • rt_list_t next;
  • uint8_t id;
  • uint8_t dir;
  • uint8_t speed;
  • }data_t;
  • //定义一些数据
  • data_t car_data = { { 0,0 },1 };
  • data_t car_data2 = { { 0,0 },2 };
  • data_t car_data3 = { { 0,0 },3 };
  • data_t car_data4 = { { 0,0 },4 };
  • rt_list_t head;
  • void list_user_example(void)
  • {
  • //初始化链表
  • rt_list_init(&head);
  • //在car_data 之后插入一个节点
  • rt_list_insert_after(&head, &car_data.next);
  • //在car_data 之后插入一个节点
  • rt_list_insert_after(&car_data.next, &car_data2.next);
  • //在car_data3之前插入一个节点
  • rt_list_insert_before(&car_data2.next, &car_data3.next);
  • //遍历每一个节点
  • rt_list_t *pos;
  • rt_list_for_each(pos, &head)
  • {
  • //获取数据结构对象的地址
  • data_t *pdata = rt_list_entry(pos, data_t, next);
  • printf("%d\r\n", pdata->id);
  • }
  • }
  • int main()
  • {
  • list_user_example();
  • return 1;
  • }

rtt_list.h[url=]复制[/url]
  • #ifndef __RTT_LIST_H__
  • #define __RTT_LIST_H__
  • #define rt_inline
  • /**
  • * Double List structure
  • */
  • struct rt_list_node
  • {
  • struct rt_list_node *next; /**< point to next node. */
  • struct rt_list_node *prev; /**< point to prev node. */
  • };
  • typedef struct rt_list_node rt_list_t; /**< Type for lists. */
  • /**
  • * @addtogroup KernelService
  • */
  • /**@{*/
  • /**
  • * rt_container_of - return the start address of struct type, while ptr is the
  • * member of struct type.
  • 给定一个结构体成员地址,返回结构体的首地址
  • */
  • #define rt_container_of(ptr, type, member) \
  • ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
  • /**
  • * @brief initialize a list object
  • 初始化链表,使nex= pre = 自己
  • */
  • #define RT_LIST_OBJECT_INIT(object) { &(object), &(object) }
  • /**
  • * @brief initialize a list
  • *
  • * @param l list to be initialized
  • 初始化链表,使nex= pre = 自己
  • */
  • rt_inline void rt_list_init(rt_list_t *l)
  • {
  • l->next = l->prev = l;
  • }
  • /**
  • * @brief insert a node after a list
  • *
  • * @param l list to insert it
  • * @param n new node to be inserted
  • 向链表某一个节点后边插入一个节点
  • */
  • rt_inline void rt_list_insert_after(rt_list_t *l, rt_list_t *n)
  • {
  • l->next->prev = n;
  • n->next = l->next;
  • l->next = n;
  • n->prev = l;
  • }
  • /**
  • * @brief insert a node before a list
  • *
  • * @param n new node to be inserted
  • * @param l list to insert it
  • 在某一个节点之前插入一个节点
  • */
  • rt_inline void rt_list_insert_before(rt_list_t *l, rt_list_t *n)
  • {
  • l->prev->next = n;
  • n->prev = l->prev;
  • l->prev = n;
  • n->next = l;
  • }
  • /**
  • * @brief remove node from list.
  • * @param n the node to remove from the list.
  • 移除一个节点
  • */
  • rt_inline void rt_list_remove(rt_list_t *n)
  • {
  • n->next->prev = n->prev;
  • n->prev->next = n->next;
  • n->next = n->prev = n;
  • }
  • /**
  • * @brief tests whether a list is empty
  • * @param l the list to test.
  • 判定链表是不是控制,就是判定是不是自己指向自己
  • */
  • rt_inline int rt_list_isempty(const rt_list_t *l)
  • {
  • return l->next == l;
  • }
  • /**
  • * @brief get the list length
  • * @param l the list to get.
  • 获取链表长度
  • */
  • rt_inline unsigned int rt_list_len(const rt_list_t *l)
  • {
  • unsigned int len = 0;
  • const rt_list_t *p = l;
  • while (p->next != l)
  • {
  • p = p->next;
  • len ++;
  • }
  • return len;
  • }
  • /**
  • * @brief 获取结构体的入口地址get the struct for this entry
  • * @param 结构体中一个成员的入口地址 node the entry point
  • * @param 结构体的名字 type the type of structure
  • * @param 结构体内部成员的名字 member the name of list in structure
  • 已知成员的地址,获取机构体的头部
  • */
  • #define rt_list_entry(node, type, member) \
  • rt_container_of(node, type, member)
  • /**
  • * rt_list_for_each - iterate over a list
  • * @param pos the rt_list_t * to use as a loop cursor.
  • * @param head the head for your list.
  • 链表遍历
  • */
  • #define rt_list_for_each(pos, head) \
  • for (pos = (head)->next; pos != (head); pos = pos->next)
  • /**
  • * rt_list_for_each_safe - iterate over a list safe against removal of list entry
  • * @param pos the rt_list_t * to use as a loop cursor.
  • * @param n another rt_list_t * to use as temporary storage
  • * @param head the head for your list.
  • */
  • #define rt_list_for_each_safe(pos, n, head) \
  • for (pos = (head)->next, n = pos->next; pos != (head); \
  • pos = n, n = pos->next)
  • /**
  • * rt_list_for_each_entry - iterate over list of given type
  • * @param pos 结构体类型的指针,用于返回结构体的入口地址 the type * to use as a loop cursor.
  • * @param head 链表头指针 the head for your list.
  • * @param 接头体成语名称 member the name of the list_struct within the struct.
  • */
  • #define rt_list_for_each_entry(pos, head, member) \
  • for (pos = rt_list_entry((head)->next, typeof(*pos), member); \
  • &pos->member != (head); \
  • pos = rt_list_entry(pos->member.next, typeof(*pos), member))
  • /**
  • * rt_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  • * @param pos the type * to use as a loop cursor.
  • * @param n another type * to use as temporary storage
  • * @param head the head for your list.
  • * @param member the name of the list_struct within the struct.
  • */
  • #define rt_list_for_each_entry_safe(pos, n, head, member) \
  • for (pos = rt_list_entry((head)->next, typeof(*pos), member), \
  • n = rt_list_entry(pos->member.next, typeof(*pos), member); \
  • &pos->member != (head); \
  • pos = n, n = rt_list_entry(n->member.next, typeof(*n), member))
  • /**
  • * rt_list_first_entry - get the first element from a list
  • * @param ptr the list head to take the element from.
  • * @param type the type of the struct this is embedded in.
  • * @param member the name of the list_struct within the struct.
  • *
  • * Note, that list is expected to be not empty.
  • */
  • #define rt_list_first_entry(ptr, type, member) \
  • rt_list_entry((ptr)->next, type, member)
  • #endif




使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

12

主题

32

帖子

1

粉丝