One
The Linux kernel list is a two-way loop linked list, similar to the linked list in the data structure, no longer detailed. Because the functions implemented in the kernel are classic, porting them is convenient for use in later applications.
/*********************************** file name: Kernel link List of linux.hbumble Bee Date: 2015-1-31 features: porting Linux kernel list ********** **************************//*linked list node data structure*/structList_head {structList_head *next, *prev;};/*********************************** function Name: Init_list_head parameter: pointer to list_head struct return value: no function function: by pointing forward and back pointers to yourself to create Build a list header ***********************************/StaticInlinevoidInit_list_head (structList_head *list) {List->next =list; List->prev =list;}/*********************************** function Name: __list_add parameter: @new: Pointer field to insert node @prev: The pointer field of the previous node @next: the second node Pointer field return value: no function: Inserting a new node in two known nodes ***********************************/StaticInlinevoid__list_add (structList_head *New, structList_head *prev,structList_head *next) {Next->prev =New; New->next =Next; New->prev =prev; Prev->next =New;}extern void__list_add (structList_head *New, structList_head *prev,structList_head *next);/************************************** function Name: List_add parameter: @new: The pointer field to insert the node @head: the pointer field to insert the list header returns a value: No function: In the known list header Inserting a new node **************************************/StaticInlinevoidList_add (structList_head *New,structList_head *head) {__list_add (New, head, head->next);}/************************************** function Name: List_add_tail parameter: @new: The pointer field to insert the node @head: the pointer field to insert the list header returns the value: No function: The end of the list is inserted into the new node **************************************/StaticInlinevoidList_add_tail (structList_head *New,structList_head *head) {__list_add (New, head->prev, head);}/************************************* function Name: List_for_each parameter: @pos: The cursor traversing the linked list @head: The return value of the table header to traverse the linked list: no function: essentially a for Looping, traversing linked list *************************************/#defineList_for_each (POS, head) for(pos = (head)->next;pos! =(head); POS= pos->next)/************************************************* function Name: list_entry parameter: @ptr: Address of List_head in node @type: type of node @member: List_head The name of the member in the struct returns the value: the address of the node, has been coerced into the Type pointer function function: the lowest position of the node is assumed to be 0, at this time the address of the member member is offset, and then List_head address Subtract offset by the address of the node **************************************************/#defineList_entry (PTR, type, member) \container_of (PTR, type, member)#defineContainer_of (PTR, type, member) ({Const typeof((Type *)0)->member) * __mptr =(PTR); (Type*)((Char*) __mptr-Offsetof (type, member)); })#defineOffsetof (Type, MEMBER) ((size_t) & ((type *) 0)->member)StaticInlinevoid__list_del (structList_head *prev,structList_head *next) {Next->prev =prev; Prev->next =next;}StaticInlinevoidList_del (structList_head *entry) {__list_del (Entry->prev, entry->next);}
Second, design the application test chain list
/**************************** file name: homework.cbumble Bee Date: 2015-1-31 function: Test the migrated Linux kernel list *************************** **/#include<stdio.h>#include"homework.h"structscore{intnum; int中文版; intMath; structlist_head list;};structScore stu1,stu2,stu3,*temp;structList_head score_head,*Pos;intMain () {Init_list_head (&score_head);//Create a linked list functionStu1.num=1; Stu1.english=0; Stu1.math=0; List_add_tail (& (Stu1.list),&(Score_head)); Stu2.num=2; Stu2.english=1; Stu2.math=1; List_add_tail (& (Stu2.list),&(Score_head)); Stu3.num=3; Stu3.english=2; Stu3.math=2; List_add_tail (& (Stu3.list),&(Score_head)); List_del (&(stu2.list)); List_for_each (POS,&(Score_head)) {Temp= List_entry (POS,structscore,list); printf ("No%d,english is%d,math is%d\n",temp->num,temp->english,temp->math); } return 0;}
Iii. Results of operation
Porting and using of Linux kernel chain list