Linux two-way linked list operation implementation tutorial, linux tutorial

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.