当前位置:网站首页>RT thread bidirectional linked list (learning notes)
RT thread bidirectional linked list (learning notes)
2022-06-28 04:12:00 【Xiaohui_ Super】
This article references from [ Wildfire EmbedFire]《RT-Thread Actual combat of Kernel Implementation and application development —— be based on STM32》, For personal study notes only . Please refer to the original text for more detailed contents and steps ( Download to the wildfire center )
List of articles
The basic concept of two-way linked list
Double linked list is also called double linked list , It's a kind of linked list , It has two pointers in each data node , Direct successor and direct precursor respectively . therefore , Starting from any node in the double linked list , Can easily access its predecessor and successor nodes . In general, we construct a bidirectional circular list .
—— Baidu Encyclopedia
This form of data structure makes it more convenient to find bidirectional linked list , Especially the traversal of a large amount of data , Because of the symmetry of the two-way linked list , It can easily complete all kinds of insertion 、 Delete and other operations , But you need to pay attention to the front and back direction of operation .
Function interface of bidirectional linked list
Linked list node structure
First introduced RT-Thread Linked list node structure in , This is no different from the two-way linked list we usually define ourselves .
/** * Double List structure */
struct rt_list_node
{
struct rt_list_node *next; /**< Tail pointer , Point to next node . */
struct rt_list_node *prev; /**< The head pointer , Point to the previous node . */
};
typedef struct rt_list_node rt_list_t; /**< Type for lists. */
Initialization of linked list rt_list_init()
The linked list must be initialized before use , Point the head and tail pointers of the linked list to yourself .
/** * @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;
}

The initialization function does not apply for space for the chain header , This step requires manual use rt_malloc() To apply for , Refer to the following code for details ( Reference to the original ).
head = rt_malloc(sizeof(rt_list_t)); // Apply dynamic memory
if(RT_NULL == head)
rt_kprintf(" Dynamic memory request failed !\n");
else
rt_kprintf(" Dynamic memory request succeeded ");
rt_kprintf(" Two way linked list initialization ......\n");
rt_list_init(head);
if(rt_list_isempty(head)
rt_kprintf(" The two-way linked list was initialized successfully !\n");
Insert a node after the specified node in the linked list rt_list_insert_after()
Before inserting a node , You need to apply for node size memory first ( See below ), Then insert a new node after the specified node .
/** * @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;
}

Insert a node before the specified node in the linked list rt_list_insert_before()
Before inserting a node , You need to apply for node size memory first , Then insert the new node before the specified node .
/** * @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;
}

Here is the sample code for the node insertion operation ( Reference from the original text ):
// Dynamically request the memory of the first node
node1 = rt_malloc(sizeof(rt_list_t));
// Dynamically request the memory of the second node
node2 = rt_malloc(sizeof(rt_list_t));
// take node2 Insert into head Back
rt_list_insert_after(head, node2);
// take node1 Insert into node2 front
rt_list_insert_before(node2, node1);
if((node1->prev == head) && (node2->prev == node1))
rt_kprintf(" Node added successfully !\n");
else
rt_kprintf(" Failed to add node !\n");
Delete a node from the linked list rt_list_remove()
Deleting a node is easier than adding a node , Just connect the two nodes before and after the node to be deleted , Release the memory of the node to be deleted .
/** * @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;
}

How to delete a node :
// The original linked list structure :head ->> node1 ->> node2 ->> node3
rt_list_remove(node2);
rt_free(node2); // Release node2 Of memory
if(node3->prev == node1) // Whether the following node is connected to the previous node
rt_kprintf(" Node deleted successfully \n");
Bidirectional linked list experiment
#include "board.h"
#include "rtthread.h"
// Define thread control block pointers
static rt_thread_t test_thread = RT_NULL;
/****************************************************************************** * @ Function name : test_thread_entry * @ work can : Thread entry function * @ ginseng Count : parameter Parameters passed in from outside * @ Return value : nothing ******************************************************************************/
static void test_thread_entry(void *parameter)
{
rt_list_t *head; // Two way chain header
rt_list_t *node1; // Node of bidirectional linked list 1
rt_list_t *node2; // Node of bidirectional linked list 2
head = rt_malloc(sizeof(rt_list_t)); // Apply dynamic memory
if(RT_NULL == head) // Failed to apply
rt_kprintf(" Dynamic memory request failed !\n");
else
rt_kprintf(" Dynamic memory request succeeded , The header node address is %d !\n", head);
rt_kprintf("\n Two way linked list initialization ......\n");
rt_list_init(head);
if(rt_list_isempty(head))
rt_kprintf(" The two-way linked list was initialized successfully !\n");
// Dynamically request the memory of the first node
node1 = rt_malloc(sizeof(rt_list_t));
// Dynamically request the memory of the second node
node2 = rt_malloc(sizeof(rt_list_t));
rt_kprintf("\n Add the first node and the second node ......\n");
// Insert separately node2 and node1, The final effect is head ->> node1 ->> node2
rt_list_insert_after(head, node2);
rt_list_insert_before(node2, node1);
if((node1->prev == head) && (node2->prev == node1))
rt_kprintf(" Node added successfully .\n");
else
rt_kprintf(" Failed to add node .\n");
rt_kprintf("\n Delete node ......\n");
rt_list_remove(node1);
rt_free(node1); // Free the memory of the first node
if(node2->prev == head)
rt_kprintf(" Node deleted successfully .\n");
while(1)
{
LED0(ON);
rt_thread_delay(500); // 500 individual tick(500ms)
LED0(OFF);
rt_thread_delay(500);
}
}
int main(void)
{
// Hardware initialization and RTT The initialization of is already in component.c Medium rtthread_startup() complete
// Create a dynamic thread
test_thread = // Thread control block pointer
rt_thread_create("list_test", // Thread name
test_thread_entry, // Thread entry function
RT_NULL, // Entry function parameters
255, // Thread stack size
5, // Thread priority
10); // Thread timeslice
// Turn on thread scheduling
if(test_thread != RT_NULL)
rt_thread_startup(test_thread);
else
return -1;
}
Experimental phenomena

边栏推荐
- How to write a software test report? Here comes the third party performance report template
- 《性能之巅第2版》阅读笔记(二)--CPU监测
- applicationContext. Getbeansoftype obtains the execution methods of all implementation classes under an interface or obtains the operation application scenarios such as implementation class objects. L
- 开关电源—Buck电路原理及其仿真
- 抖音实战~关注博主
- 光伏板怎么申请ASTM E108阻燃测试?
- leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】
- ERP升级的另一种选择,MES系统
- English语法_形容词/副词3级-比较级_常用短语
- 指针链表
猜你喜欢

Learning about DC-DC step-down chip of sy8120i (12V reduced to 3.3V)

Analysis of future teacher research ability under steam education framework

From zero to one, I will teach you to build a "search by text and map" search service (I)

美创入选“2022 CCIA中国网络安全竞争力50强”榜单

电学基础知识整理(二)

Problems with cat and dog queues

Leetcode: monotonic stack structure (Advanced)

TFTLCD display experiment of mini plate based on punctual atom stm32
![[small program practice series] e-commerce platform source code and function implementation](/img/a4/581d73c2ff5c190edc3d8438fa2122.png)
[small program practice series] e-commerce platform source code and function implementation

@Several scenarios of transactional failure
随机推荐
MSc 307 (88) (2010 FTPC code) Part 9 bedding test
English grammar_ Adjective / adverb Level 3 - Comparative
Une seule pile dans l'ordre inverse avec des fonctions récursives et des opérations de pile
Using elk to build a log analysis system (I) -- component introduction
MSc 307 (88) (2010 FTPC code) Part 2 smoke and toxicity test
05 MongoDB对列的各种操作总结
电学基础知识整理(二)
测不准原理
01 overview, application scenarios, Download methods, connection methods and development history of mongodb
【Linux】【Mysql】ERROR 1698 (28000): Access denied for user ‘root‘@‘localhost‘
[small program practice series] e-commerce platform source code and function implementation
From zero to one, I will teach you to build a "search by text and map" search service (I)
Genicam gentl standard ver1.5 (2)
Talking about cloud primitiveness, we have to talk about containers
开关电源—Buck电路原理及其仿真
[MySQL] multi table connection query
Chapter 14 AC-DC power supply front stage circuit note I
几个重要的物理概念
Conversion between decimal and BCD codes in C language
等保三级密码复杂度是多少?多久更换一次?