正常表達html
非通用鏈表自理解概念:節點攜帶信息node
通用鏈表自理解概念:信息攜帶節點git
通用鏈表與非通用鏈表的區別數據結構
/* *Structure of a node in a doubly linked list. */ typedef struct LSS_LIST { struct LSS_LIST *pstPrev; /**< Current node's pointer to the previous node*/ struct LSS_LIST *pstNext; /**< Current node's pointer to the next node*/ } LSS_LIST; typedef struct LSS_LIST listItem_t;
鏈表ui
信息訪問3d
/* * @param item Current node's pointer. * @param type Structure name of type. * @param member Member name of the doubly linked list in the structure. */ #define LSS_LIST_ENTRY(item, type, member) \ ((type *)((char *)(item) - (unsigned long)(&((type *)0)->member)))
/** * @brief 鏈表初始化 * @param pstList:須要初始化的鏈表(節點)指針 * @retval none * @author lzm */ void listInit(listItem_t *pstList) { pstList->pstNext = pstList; pstList->pstPrev = pstList; }
/** * @brief 獲取第一個節點 * @param pstObject:當前節點指針 * @retval none * @author lzm */ #define listGetFirst(pstObject) ((pstObject)->pstNext)
/** * @brief 插入當前節點後面 * @param pstList:鏈表(也是當前節點) * @param pstNode:節點(須要插入的節點) * @retval none * @author lzm */ void listAdd(LSS_LIST *pstList, LSS_LIST *pstNode) { pstNode->pstNext = pstList->pstNext; pstNode->pstPrev = pstList; pstList->pstNext->pstPrev = pstNode; pstList->pstNext = pstNode; }
/** * @brief 插入鏈表尾部 * @param pstList:鏈表(也是當前節點) * @param pstNode:節點(須要插入的節點) * @retval none * @author lzm */ void listTailInsert(LSS_LIST *pstList, LSS_LIST *pstNode) { listAdd(pstList->pstPrev, pstNode); // 把當前節點的前一個節點做爲參考便可 }
/** * @brief 刪除當前節點 * @param pstNode:節點(須要刪除的節點) * @retval none * @author lzm */ void listDelete(LSS_LIST *pstNode) { pstNode->pstNext->pstPrev = pstNode->pstPrev; pstNode->pstPrev->pstNext = pstNode->pstNext; pstNode->pstNext = (LSS_LIST *)NULL; pstNode->pstPrev = (LSS_LIST *)NULL; }
/** * @brief 刪除當前節點 * @param pstNode:節點(須要刪除的節點) * @retval TRUE:鏈表爲空 * @retval FALSE:鏈表不爲空 * @author lzm */ bool listEmpty(LSS_LIST *pstNode) { return (bool)(pstNode->pstNext == pstNode); }
/** * @brief 獲取到信息句柄的偏移 * @param type:信息結構體類型 * @param member:成員名字,便是字段(域) * @retval 偏移長度(單位:byte) * @author lzm */ #define getOffsetOfMenber(type, member) ((uint32_t)&(((type *)0)->member))
/** * @brief 獲取節點所在的信息句柄 * @param type:信息結構體類型 * @param member:成員名字,便是字段(域) * @retval 返回節點所在的信息句柄 * @author lzm */ #define getItemDataHandle(item, type, member) \ ((type *)((char *)item - getOffsetOfMenber(type, member))) \
/** * @brief 刪除節點並從新初始化 * @param pstList:須要從新初始化的鏈表節點 * @retval * @author lzm */ #define LIST_FOR_EACH(item, list) \ for ((item) = (list)->pstNext; \ (item) != (list); \ (item) = (item)->pstNext)
/** * @brief 遍歷整個鏈表並得到信息句柄(宏) * @param handle:保存目標節點信息句柄 * @param item:須要遍歷的鏈表(節點) * @param type:信息類型(結構體名) * @param member:該鏈表在 type 中的名字 * @retval 就是也該for語句 * @author lzm */ #define LIST_FOR_EACH_HANDEL(handle, list, type, member) \ for (handle = getItemDataHandle((list)->pstNext, type, member); \ &handle->member != (list); \ handle = getItemDataHandle(handle->member.pstNext, type, member))
void osListDel(LSS_LIST *pstPrevNode, LSS_LIST *pstNextNode) { pstNextNode->pstPrev = pstPrevNode; pstPrevNode->pstNext = pstNextNode; } /** * @brief 刪除節點並從新初始化 * @param pstList:須要從新初始化的鏈表節點 * @retval * @author lzm */ void listDelInit(LSS_LIST *pstList) { osListDel(pstList->pstPrev, pstList->pstNext); listInit(pstList); }