<> Bidirectional linked list

1. Create a structure of a two-way linked list , There are two pointers in it , It can point to two adjacent nodes before and after
/*! *\brief Bidirectional linked list node structure */ typedef struct list_node { struct list_node* next;
struct list_node* previous; }list_item_t;
2. Bidirectional linked list initialization
/*! * \brief Bidirectional linked list initialization * * \param Chain header node * * \return nothing * * \note nothing * * \see * *
\date 2019/12/08 16:57 */ void list_init(list_item_t* list_head) { list_head->
next= list_head; list_head->previous = list_head; }
3. Insert a node into a two-way linked list
/*! * \brief A bidirectional linked list inserts a node after a node * * \param struct Node * * \return nothing * * \note nothing * *
\see * * \date 2019/12/08 16:57 */ void list_insert_after(list_item_t* l,
list_item_t* n) { l->next->previous = n; n->next = l->next; l->next = n; n->
previous= l; } /*! * \brief A bidirectional linked list inserts a node in front of a node * * \param struct Node * * \return nothing * *
\note nothing * * \see * * \date 2019/12/08 16:57 */ void list_insert_before(
list_item_t* l, list_item_t* n) { l->previous->next = n; n->previous = l->
previous; l->previous = n; n->next = l; }
4. Delete a node
/*! * \brief Delete a node from a two-way linked list * * \param struct Node * * \return nothing * * \note nothing * * \see * *
\date 2019/12/08 16:57 */ void list_remove(list_item_t* l) { l->previous->next =
l->next; l->next->previous = l->previous; l->next = l->previous = l; }
5. Other functions
/*! * \brief Check whether the two-way linked list is empty * * \param struct Node * * \return 1: empty 0: Non empty * * \note nothing * *
\see * * \date 2019/12/08 16:57 */ uint8_t list_isempty(list_item_t* l) { return
(uint8_t)(l->next == l); } /*! * \brief Get the length of bidirectional linked list * * \param struct Node * * \return length
* * \note nothing * * \see * * \date 2019/12/08 16:57 */ uint32_t list_getlen(
list_item_t* l) { uint32_t len = 0; const list_item_t* p = l; while (p->next !=
l) { p = p->next; len++; } return len; }
<> Use of two-way linked list

There are not many functions in a two-way linked list , Other applications are added on the basis of the above functions .
There are two common uses , The first is to change the two-way linked list structure , Add data and other functions , Then operate on this basis
/*! *\brief Bidirectional linked list node structure */ typedef struct list_node { struct list_node* next;
struct list_node* previous; uint32_t data; }list_item_t;
The other is to directly use the double linked list function above , Reconstruct new data structures as needed , For example, construct a student data structure , Use the double linked list as a hook .
typedef struct student { list_item_t list; uint32_t grade; char name[20]; float
score; }student_t;

But in that case , You can use the list variable , Links to the upper and lower student structures list member , But what we need to operate on is the student variable , Therefore, we need to list Member Address , Find the address of the student structure ( This is linux/rtt Common operations in the system ).
/** * according to list Address of Gets the base address of the structure */ #define rt_container_of(ptr, type, member) \
((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
establish 100 Student data structure
uint8_t grade_init(student_t* head, uint32_t num) { student_t* pstudent;
list_init(&head->list); // Initialize header node head->score = (float)num; head->grade = 0;
memset(head->name, 0, offsetof(student_t, score) - offsetof(student_t, name));
while (num--) { pstudent = (student_t*)malloc(sizeof(student_t)); if (pstudent
== NULL) { return 1; // Failed to allocate memory } list_insert_after(&head->list, &pstudent->list);
pstudent->score = 0; pstudent->grade = 0; memset(pstudent->name, 0, offsetof(
student_t, score) - offsetof(student_t, name)); } return 0; } int main(void) {
printf("hello world!\n"); student_t * student_head = (student_t *)malloc(sizeof(
student_t)); uint8_t err = grade_init(student_head, 100); if (err != 0) { printf
(" initialization failed \n"); } /* Access upper and lower nodes through hook linked list */ student_t * student_head3 = rt_container_of(
student_head->list.next->next->next, student_t, list); student_head3->score =
18.756; /* vs use strcpy Shielding warning required */ #pragma warning(disable : 4996) strcpy(
student_head3->name, " Zhang San "); while (1) { } return 0; }

Technology
©2019-2020 Toolsou All rights reserved,
Android Using wechat in H5 Payment result refresh during payment shock !!C++ Can make a sound ! Basic operation of single linked list C Language explanation Java Implement an epidemic number management system 2021 year 11 World programming language ranking linux upper mysql Invalid command _linux lower mysql The command is useless Java project : Campus dormitory management system (java+jsp+javaweb+mysql+ajax) Wechat applet development project directory linux ubuntu Which version ,Ubuntu Which version is the best ?python Code painting cherry blossoms - How to use Python Draw a beautiful cherry blossom