雙向鏈表的理解

背景

代碼中經常會出現雙向鏈表,對于雙向鏈表的插入和刪除有對應的API函數接口,但直觀的圖表更容易理解,所以本文會對rt-thread內核代碼中提供的雙向鏈表的一些API函數操作進行繪圖,方便后續隨時查看。

代碼塊

rt-thread中提供的代碼段包括:
鏈表定義rtdef.h

/*** 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. */

API操作定義rtserver.h

/*** @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*/
#define RT_LIST_OBJECT_INIT(object) { &(object), &(object) }/*** @brief initialize a list** @param l list to be initialized*/
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* @pos:    the rt_list_t * to use as a loop cursor.* @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* @pos:    the rt_list_t * to use as a loop cursor.* @n:      another rt_list_t * to use as temporary storage* @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* @pos:    the type * to use as a loop cursor.* @head:   the head for your list.* @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* @pos:    the type * to use as a loop cursor.* @n:      another type * to use as temporary storage* @head:   the head for your list.* @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* @ptr:    the list head to take the element from.* @type:   the type of the struct this is embedded in.* @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)

重點函數

rt_list_init()
rt_list_insert_after()
rt_list_insert_before()
rt_list_remove()

重點函數理解

rt_list_init(rt_list_t *l)

rt_inline void rt_list_init(rt_list_t *l)
{l->next = l->prev = l;
}

初始化當前鏈表l,即當前鏈表l的pre和next都是指向自己。
在這里插入圖片描述

rt_list_insert_after(rt_list_t *l, rt_list_t *n)

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;
}

將新鏈表n1插入到l之后
在這里插入圖片描述
將新鏈表n2插入到l之后
在這里插入圖片描述
將新鏈表n3插入到l之后
在這里插入圖片描述

rt_list_insert_before(rt_list_t *l, rt_list_t *n)

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;
}

將新鏈表n1插入到l之前
在這里插入圖片描述
將新鏈表n2插入到l之前
在這里插入圖片描述

rt_list_remove(rt_list_t *n)

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;
}

從已有鏈表中移除當前鏈表,如原鏈表
在這里插入圖片描述
從此鏈表中移除n2,則
在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/76614.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/76614.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/76614.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

大文件上傳源碼,支持單個大文件與多個大文件

大文件上傳源碼&#xff0c;支持單個大文件與多個大文件 Ⅰ 思路Ⅱ 具體代碼前端--單個大文件前端--多個大文件前端接口后端 Ⅰ 思路 具體思路請參考我之前的文章&#xff0c;這里分享的是上傳流程與源碼 https://blog.csdn.net/sugerfle/article/details/130829022 Ⅱ 具體代碼…

Unity中的靜態合批使用整理

靜態批處理是一種繪制調用批處理方法&#xff0c;它組合不移動的網格以減少繪制調用。它將組合的網格轉換為世界空間&#xff0c;并為它們構建一個共享頂點和索引緩沖區。然后&#xff0c;對于可見網格&#xff0c;Unity 會執行一系列簡單的繪制調用&#xff0c;每個調用之間幾…

【機器學習中的基本術語:特征、樣本、訓練集、測試集、監督/無監督學習】

機器學習基本術語詳解 1. 特征&#xff08;Feature&#xff09; 定義&#xff1a;數據的屬性或變量&#xff0c;用于描述樣本的某個方面。作用&#xff1a;模型通過學習特征與目標之間的關系進行預測。示例&#xff1a; 預測房價時&#xff0c;特征可以是 面積、地段、房齡。…

C++學習之路:指針基礎

目錄 指針介紹與基本用法雙重指針函數指針空指針與野指針函數參數的指針傳遞最后 指針一般在C/C語言學習的后期接觸&#xff0c;這樣就導致指針給新手一種高深莫測、難以掌握的刻板印象。但實際上指針的使用很簡單&#xff0c;并且還能夠極大的提高程序的靈活性&#xff0c;幫助…

【服務日志鏈路追蹤】

MDCInheritableThreadLocal和spring cloud sleuth 在微服務架構中&#xff0c;日志鏈路追蹤&#xff08;Logback Distributed Tracing&#xff09; 是一個關鍵需求&#xff0c;主要用于跟蹤請求在不同服務間的調用鏈路&#xff0c;便于排查問題。常見的實現方案有兩種&#x…

Kafka+Zookeeper從docker部署到spring boot使用完整教程

文章目錄 一、Kafka1.Kafka核心介紹&#xff1a;?核心架構?核心特性?典型應用 2.Kafka對 ZooKeeper 的依賴&#xff1a;3.去 ZooKeeper 的演進之路&#xff1a;注&#xff1a;&#xff08;本文采用ZooKeeper3.8 Kafka2.8.1&#xff09; 二、Zookeeper1.核心架構與特性2.典型…

JUC系列JMM學習之隨筆

JUC: JUC 是 Java 并發編程的核心工具包,全稱為 Java Util Concurrent,是 java.util.concurrent 包及其子包的簡稱。它提供了一套強大且高效的并發編程工具,用于簡化多線程開發并提高性能。 CPU核心數和線程數的關系:1核處理1線程(同一時間單次) CPU內核結構: 工作內…

The Rust Programming Language 學習 (九)

泛型 每一個編程語言都有高效處理重復概念的工具。在 Rust 中其工具之一就是 泛型&#xff08;generics&#xff09;。泛型是具體類型或其他屬性的抽象替代。我們可以表達泛型的屬性&#xff0c;比如他們的行為或如何與其他泛型相關聯&#xff0c;而不需要在編寫和編譯代碼時知…

藍橋杯 混乘數字

問題描述 混乘數字的定義如下&#xff1a; 對于一個正整數 n&#xff0c;如果存在正整數 a 和 b&#xff0c;使得&#xff1a; n a b且 a 與 b 的十進制數位中每個數字出現的次數之和&#xff0c;與 n 中對應數字出現的次數相同&#xff0c;則稱 n 為混乘數字。 示例 對于…

CExercise04_1位運算符_2 定義一個函數判斷給定的正整數是否為2的冪

題目&#xff1a; 給定一個正整數&#xff0c;請定義一個函數判斷它是否為2的冪(1, 2, 4, 8, 16, …) 分析&#xff1a; &#xff1a; 代碼 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdbool.h>/* 給定一個正整數&#xff0c;請定義一個函數…

SSL證書不可信的原因有哪些?(國科云)

SSL證書用于在客戶端和服務器端之間建立一條加密通道&#xff0c;確保數據在傳輸過程中的安全性和完整性。然而&#xff0c;在實際應用中&#xff0c;我們有時會遇到SSL證書不可信的情況&#xff0c;嚴重影響了用戶對網站的信任度。那么&#xff0c;SSL證書不可信的原因究竟有哪…

[王陽明代數講義]琴語言類型系統工程特性

琴語言類型系統工程特性 層展物理學組織實務與藝術與琴生生.物機.械科.技工.業研究.所軟凝聚態物理開發工具包社會科學氣質砥礪學人生意氣場社群成員魅力場與心氣微積分社會關系力學 意氣實體過程圖論信息編碼&#xff0c;如來碼導引 注意力機制道裝Transformer架構的發展標度律…

自抗擾ADRC之二階線性擴展狀態觀測器(LESO)推導

1.龍伯格觀測器 實際工程應用中&#xff0c;狀態變量有時難以使用傳感器直接測量&#xff0c;在這種情況下&#xff0c;使用狀態觀測器估計系統實際狀態是非常常見的做法。最出名的狀態觀測器當屬龍伯格博士在1971年發表于TAC的An Introduction to Observer[1]一文中提出的基于…

從頭開發一個Flutter插件(二)高德地圖定位插件

開發基于高德定位SDK的Flutter插件 在上一篇文章里具體介紹了Flutter插件的具體開發流程&#xff0c;從創建項目到發布。接下來將為Flutter天氣項目開發一個基于高德定位SDK的Flutter定位插件。 申請key 首先進入高德地圖定位SDK文檔內下載定位SDK&#xff0c;并按要求申請A…

分布式鎖之redis6

一、分布式鎖介紹 之前我們都是使用本地鎖&#xff08;synchronize、lock等&#xff09;來避免共享資源并發操作導致數據問題&#xff0c;這種是鎖在當前進程內。 那么在集群部署下&#xff0c;對于多個節點&#xff0c;我們要使用分布式鎖來避免共享資源并發操作導致數據問題…

ubuntu中使用安卓模擬器

本文這里介紹 使用 android studio Emulator &#xff0c; 當然也有 Anbox (Lightweight)&#xff0c; Waydroid (Best for Full Android Experience), 首先確保自己安裝了 android studio &#xff1b; sudo apt update sudo apt install openjdk-11-jdk sudo snap install…

二語習得理論(Second Language Acquisition, SLA)如何學習英語

二語習得理論&#xff08;Second Language Acquisition, SLA&#xff09;是研究學習者如何在成人或青少年階段學習第二語言&#xff08;L2&#xff09;的理論框架。該理論主要關注語言習得過程中的認知、社會和文化因素&#xff0c;解釋了學習者如何從初學者逐漸變得流利并能夠…

WinDbg. From A to Z! 筆記(下)

原文鏈接: WinDbg. From A to Z! 文章目錄 使用WinDbg臨界區相關命令示例 -- 查看臨界區其他有用的命令 WinDbg中的偽寄存器自動偽寄存器 WinDbg中的表達式其他操作默認的表達式計算方式 WinDbg中的重命名調試器命令語言編程控制流命令程序執行 WinDbg 遠程調試事件監控WinDbg …

RainbowDash 的旅行

D RainbowDash 的旅行 - 第七屆校賽正式賽 —— 補題 題目大意&#xff1a; 湖中心有一座島&#xff0c;湖的外圍有 m m m 間木屋&#xff08;圍繞小島&#xff09; &#xff0c;第 i i i 間木屋和小島之間有 a i a_i ai? 座 A A A 類橋&#xff0c; b i b_i bi? 座 B …

MySQL-SQL-DDL語句、表結構創建語句

一.SQL SQL&#xff1a;一門操作關系型數據庫的編程語言&#xff0c;定義操作所有關系型數據庫的統一標準 二. DDL-數據庫 1. 查詢所有數據庫 命令&#xff1a;show databases; 2. 查詢當前數據庫 命令&#xff1a;select database(); 3. 創建數據庫 命令&#xff1a;create da…