A simple example of a two-way linked list head that learns Linux with cainiao

Source: Internet
Author: User


I often see the list head in the Linux kernel. Sometimes I am not very familiar with it. I plan to understand it very early. After compiling the SDK this time, I am a little idle. Let's just take it out.


Simple Linked List Implementation

#include <stdio.h>#include <stdlib.h>#include <string.h>struct list_head{       struct list_head *next, *prev;}; #define INIT_LIST_HEAD(ptr) do{ \       (ptr)->next = (ptr); (ptr)->prev = (ptr); \       }while(0)      #define list_for_each(pos, head) \      for (pos = (head)->next; pos != (head); pos = pos->next) #define offsetof(TYPE, MEMBER) ((size_t)& ((TYPE *)0)->MEMBER)     #define container_of(ptr, type, member) ({\       const typeof( ((type *)0)->member ) *__mptr = (ptr);\          (type*)((char *)__mptr - offsetof(type,member));})   #define list_entry(ptr, type, member) \       container_of(ptr, type, member) #define list_for_each_safe(pos, n, head) \      for (pos = (head)->next, n = pos->next; pos != (head); \              pos = n, n = pos->next) static inline void __list_del(struct list_head * prev, struct list_head * next){       next->prev = prev;       prev->next = next;} static inline void __list_del_entry(struct list_head *entry){       __list_del(entry->prev, entry->next);}static inline void list_del_init(struct list_head *entry){      __list_del_entry(entry);      INIT_LIST_HEAD(entry);}      static inline void __list_add(struct list_head *new1,                           struct list_head *prev,                          struct list_head *next){       next->prev = new1;       new1->next = next;       new1->prev = prev;       prev->next = new1;} static inline void list_add(struct list_head *new1, struct list_head *head){       __list_add(new1, head, head->next);} struct person{       int age;       int weight;       struct list_head list; };int main(void){       struct person *tmp;       struct list_head *pos, *n;       int age_i, weight_j;       struct person person_head;       INIT_LIST_HEAD(&person_head.list);           for(age_i = 10, weight_j = 35; age_i < 40; age_i += 5, weight_j +=5)       {              tmp = (struct person *)malloc(sizeof(struct person));              tmp->age = age_i;              tmp->weight = weight_j;              list_add(&(tmp->list), &person_head.list);       }            printf("\n");       printf("============ print the list =============\n");       list_for_each(pos, &person_head.list)       {              tmp = list_entry(pos, struct person, list);              printf("age:%d,      weight:    %d \n", tmp->age, tmp->weight);       }       printf("\n");                     printf("======== print list after delete a node which age is 15 =======\n");       list_for_each_safe(pos, n, &person_head.list)       {              tmp = list_entry(pos, struct person, list);              if(tmp->age== 15)              {                     list_del_init(pos);                     free(tmp);              }       }       list_for_each(pos, &person_head.list)       {              tmp = list_entry(pos, struct person, list);              printf("age:%d,      weight:%d\n", tmp->age, tmp->weight);       }       return 0;}

 

Next, let's analyze the functions and macro definitions in this list head.

1. init_list_head

This is the initialization of the linked list header, and the Pointer Points to itself.

2. list_for_each

This is to traverse the function, and look at the function prototype to know that it is a for loop. It is no big difference from the traversal of a common single-chain table.

3. container_of

This has been written in previous blogs.

4. list_entry

This is the container_of above. The specific meaning is to get the pointer of the entire defined struct according to the pointer of the List header.

5. list_for_each_safe

This is the same as the list_for_each function, but it only has an n intermediate variable. Because the POs pointer can be deleted, the point of the POs will change, the N variable is introduced, so there will be no errors and it will be safe. So here we have a safe option.

6. list_del_init

Delete a node

7. list_add

Insert a node

 

The main functions of the function are also completed, and the implementation method can also be shown according to the main function. Some functions and macros are used here. For details, refer to Linux/list. h. I almost know that this is enough. I will try again when I encounter problems.

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.