The data structure of a two-way linked list is usually implemented:
StructList_node1 {StructList_node1 * Next ,*Prev; type1 M1; Type 2 m2 ;};StructList_node2 {StructList_node2 * Next ,*Prev; type1 M1; Type 2 m2 ;};......
Each data structure defines its specific structure for implementing the linked list and the corresponding method (Add/del) for operating the linked list;
However, for systems with a large number of different data structures, you must use the linked list. If you define a specific structure and operation method for each data structure,
UndoubtedlyCodeAs it becomes repetitive and bloated, a common two-way linked list method is required, which is applicable to various data structures.
C language does not contain templates in C ++. How can this problem be achieved?
The Linux kernel uses the following data structure to implement a two-way linked list:
StructList_head {StructList_head * Next ,*Prev ;};
If you need a two-way queue with a certain data structure, put a list_head data structure member in this structure.
StructKobject {Const Char*Name;StructList_head entry;StructKobject *Parent;StructKset *Kset ;}
The following structure is formed:
How can I access the next member through the list_head member entry in the kobject linked list structure?
The system provides the macro list_entry:
# DefineList_entry (PTR, type, member )\(Type*)((Char*) (PTR)-(unsignedLong) (& (Type *)0)->Member )))
// Access the Linked List member kobject* OBJ =Objlist; kobject* Nextobj = (kobject *) list_entry (obj-> entry-> next,StructKobject, entry );
What is the list_entry principle:
PTR: Indicates the data member entry in the current kobject structure object. (char *) (PTR): Entry Member address --Addra;
(Unsigned long) (& (type *) 0)-> member )): Convert address 0 to type (struct kobject) object, take the address of the member (entry) member --Addrb.
(Type *) (addra-addrb): Get the first address of the kobject structure object and convert it to the kobject object.
Here we need to pay attention to addrb:Convert address 0 to type (struct kobject) object and take the address of the member (entry) member.
It indicates that when the first address of the struct kobject object is 0, the address of the member Member (entry) is obtained, which is offset from the first address.
The first address of the struct kobject object can be calculated based on the member (entry) address in the struct kobject object and the offset relative to the first address.
Structure:
Fully utilizes the features of Direct Memory Address operations in C language.