Linux two-way linked list operation implementation tutorial, linux tutorial
Linux bidirectional linked list operation implementation tutorial
The code is located in include/linux/list. h. The linked list is initialized:
static inline void INIT_LIST_HEAD(struct list_head *list){ list->next = list; list->prev = list;}
After initialization, the status chart of the linked list is shown in the following figure: prev pointer on the left and next pointer on the right:
Use the list_add_tail function to insert a node for the first time. Code:
static inline void list_add_tail(struct list_head *new, struct list_head *head){ __list_add(new, head->prev, head);}static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next){ next->prev = new; new->next = next; new->prev = prev; prev->next = new;}
The linked list after new1 is inserted on the first node:
The following figure shows how to continue calling the list_add_tail function to insert new2 to the second node:
Circular code of a two-way linked list:
#define list_for_each(pos, head) \ for (pos = (head)->next; pos != (head); pos = pos->next)
The order of loop processing is the same as that of list_add_tail. It starts with the next pointer of the header, namely node new1, followed by node new2, until the header of the node.