A two-way linked list list_head (lower) of Linux kernel source code)

Source: Internet
Author: User

Original article. Please indicate the source for reprinting. Thank you!
Author: Qinglin, blog name: feikong jingdu

 

I use a program to describe in struct
Person
Added struct
List_head
How to operate such a two-way linked list after the variable.

 

# Include <stdio. h> <br/> # include "list. H "<br/> struct person <br/>{< br/> int age; <br/> int weight; <br/> struct list_head list; <br/>}; <br/> int main (INT argc, char * argv []) <br/>{< br/> struct person * TMP; <br/> struct list_head * POs, * n; <br/> int age_ I, weight_j; <br/> // define and initialize a linked list header <br/> struct person person_head; <br/> init_list_head (& person_head.list ); </P> <p> for (age_ I = 10, weight_j = 35; age_ I <40; age_ I + = 5, weight_j + = 5) <br/>{< br/> TMP = (struct person *) malloc (sizeof (struct person); <br/> TMP-> age = age_ I; <br/> TMP-> Weight = weight_j; <br/> // link the node to the end of the linked list <br/> // This is because each node is added to the end of the person_head, therefore, the first added node is displayed at the end of the linked list. <br/> // The order shown during the printing is that the added node is printed at the end. <br/> list_add (& (TMP-> list ), & (person_head.list); <br/>}< br/> // print the values of each node in the linked list <br/> printf ("/N "); <br/> printf ("============= print the list ===========================/N "); <br/> list_for_each (Pos, & person_head.list) <br/>{< br/> // here we use list_entry to obtain the pointer of the POs structure <br/> TMP = list_entry (Pos, struct person, list ); <br/> printf ("Age: % d, weight: % d/N", TMP-> Age, TMP-> weight ); <br/>}< br/> printf ("/N"); <br/> // delete a node, node whose age is 20 <br/> printf ("=========== print list after delete a node which age is 20 ======== =/N "); <br/> list_for_each_safe (Pos, N, & person_head.list) <br/> {<br/> TMP = list_entry (Pos, struct person, list ); <br/> If (TMP-> age = 20) <br/>{< br/> list_del_init (POS); <br/> free (TMP ); <br/>}< br/> list_for_each (Pos, & person_head.list) <br/>{< br/> TMP = list_entry (Pos, struct person, list); <br/> printf ("Age: % d, weight: % d/N", TMP-> Age, TMP-> weight ); <br/>}< br/> // release resources <br/> list_for_each_safe (Pos, N, & person_head.list) <br/>{< br/> TMP = list_entry (Pos, struct person, list); <br/> list_del_init (POS); <br/> free (TMP ); <br/>}</P> <p> return 0; <br/>}

 

Compile:

 

Linux
Under
Yes: gcc-g-Wall Main. C-o
Test

Windows
You can create a console project under
And list. h
Add it to the Project for compilation.

 

Run Test
The output is as follows:

==============
Print the list ======================

Age: 35,
Weight: 60

Age: 30,
Weight: 55

Age: 25,
Weight: 50

Age: 20,
Weight: 45

Age: 15,
Weight: 40

Age: 10,
Weight: 35

 

============
Print list after delete a node which age is 20 ============

Age: 35,
Weight: 60

Age: 30,
Weight: 55

Age: 25,
Weight: 50

Age: 15,
Weight: 40

Age: 10,
Weight: 35

 

We can see that this is a very good and effective two-way linked list. We do not need to define related functions for each structure, such as traversing, adding, and deleting functions, we only need to simply add struct to the structure.
List_head
As a variable, our structure immediately becomes a two-way linked list, and we do not need to write the operations on the linked list, directly calling the defined functions and macros, everything is so simple and effective.

 

Should I end the article here? No, I don't want to end it. I want to continue.

 

IV,
Multiple linked lists in one structure

 

We can see that the human structure is as follows:

Struct
Person

{

Int
Age;

Int
Weight;

Struct
List_head list;

};

 

Its linked list chart looks like this:

But we know that there are still many people who are familiar with a person, for example, he has various kinds of clothes and various kinds of shoes. Therefore, I have defined two such structures:

 

Struct
Clothes

{

Int
Size ;//
Clothes of various sizes

Color
Color ;//
Clothes have various colors. Here we assume there is
Color
Type

};

 

Struct
Shoot

{

Kind
Kind ;//
There are various types of shoes, such as autumn, winter, sports, and leisure. It is also assumed that
Kind
This type

Color
Color ;//
The shoes also have various colors.

};

 

The definition of this person may be as follows:

Struct
Person

{

Int
Age;

Int
Weight;

Struct
Clothes clo;

Struct
Shoot SHT;

};

 

Here, I intentionally
Clo
And
Sht
These two variables are placed in
List
In fact
List
It does not matter where the position in the structure is located,
List_entry
You can also find the pointer of the structure.

 

One problem here is that a person has more than one dress and more than one pair of shoes, so we should add the clothes and shoes he owns. How can we add them? Here we should change the structure of clothes and shoes into a linked list.

 

Change the structure to the following:

 

Struct
Clothes

{

Struct
List_head list;

Int
Size ;//
Clothes of various sizes

Color
Color ;//
Clothes have various colors. Here we assume there is
Color
Type

};

 

Struct
Shoot

{

Struct
List_head list;

Kind
Kind ;//
There are various types of shoes, such as autumn, winter, sports, and leisure. It is also assumed that
Kind
This type

Color
Color ;//
The shoes also have various colors.

};

 

Now the shoes and clothes are linked lists. You can connect them. Should our structure be defined as follows:

Struct
Person

{

Int
Age;

Int
Weight;

Struct
Clothes clo;

Struct
Shoot SHT;

};

 

If so, how should we define this header node. As we can see earlier, define
Person_head
The header node of is as follows:

 

//

Define and initialize a linked list Header

Struct
Person person_head;

Init_list_head

(& Person_head.list );

 

Should we define it like this?

 

//

Define and initialize a linked list Header

Struct
Person person_head;

Init_list_head

(& Person_head.list );

Init_list_head

(& Person_head.col.list );

Init_list_head

(

&
Person_head.sht.list );

 

Then add a dress. The code looks like this:

 

 

Struct
Clothes TMP = (struct clothes *) malloc (sizeof (struct clothes ));

......

List_add (& (TMP-> list ),
& (Person_head.clo.list ));

 

This will not be a bit troublesome. In fact, if we can think about it seriously, we will find that since
Struct
Peron
Is
List_head
It can link its type nodes to the back, so
Struct
Clothes
It also contains
List_head
They have no difference in nature. They can also be linked to it. So our
Struct
Person
The structure of should be like this:

Struct
Person

{

Int
Age;

Int
Weight;

Struct
List_head clo;

Struct
List_head SHT;

};

 

The figure after the node is linked is shown in:

<! --
@ Page {margin: 2 cm}
P {margin-bottom: 0.21}
-->

From the above, we can know that
Struct
List_head
Structure, we can add multiple subnode linked lists for our struct.

 

The PDF version and code of this article can be downloaded through the following connection:

In simple terms, the listlinux 源 listlist_head.zip

 

 

 

 

 

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.