Linux Kernel linked list (3), Linux Kernel

Source: Internet
Author: User

Linux Kernel linked list (3), Linux Kernel

Introduction to common Linux kernel two-way linked list APIs

The linux link list structure is as follows:



Position of the kernel bidirectional linked list in the linux kernel:/include/linux/list. h

The process of using a two-way linked list includes creating a struct (item) containing the struct link_head structure, creating a linked list header, and adding an item (custom data structure, two-way linked list data unit), delete linked list nodes, traverse chain tables, empty judgment, and so on.

1. Create a custom Linked List Data Structure

  1. Struct kool_list {
  2. Int;
  3. Struct list_head list; // contains the linked list header.
  4. Int from;
  5. }; // Customize the data amount structure to be linked and contain a two-way linked list Structure
2. Create a linked list Header

  1. Struct kool_list mylist;
  2. INIT_LIST_HEAD (& mylist. list); // initialize a table header.
Or

Static LIST_HEAD (adc_host_head); // initialize a linked list header, adc_host_head

The second method creates a chain table header. The first method is different from the first method. The second method is initialized during compilation. Another point is whether the linked list header is independent or in the Custom linked list data structure. It is like the difference between the linked list structure shown in Linux kernel linked list (2) and that shown in this article.

3. add an item to the linked list

List_add (struct list_head * new, struct list_head * head );

For example:

  1. Struct kool_list * tmp;

  2. Tmp = (struct kool_list *) malloc (sizeof (struct kool_list ));
  3. Printf ("enter to and from :");
  4. Scanf ("% d", & tmp-> to, & tmp-> from); // initialize the Data Structure
  5. /* Add the new item 'tmp 'to the list of items in mylist */
  6. List_add (& (tmp-> list), & (mylist. list); // Add a new element node to the linked list. list in tmp

List_add_tail (struct list_head * new, struct list_head * head );

Add an item at the end of the linked list. The difference between list_add and list_add is that list_add adds the new item to the end of the linked list header, and list_add_tail adds the item to the front of the linked list header.

4. traversal table

List_for_each_entry (type * cursor, struct list_head * list, member)

This is not a function. It is a for loop, listing the linked list to be traversed in turn. The meaning of the three elements: type * cursor represents the pointer of the item, struct list_head * list is the head of the linked list, and member is the list_head data item contained in the item. The three data items can be used to locate each data element in the linked list. The positioning principle is the structure offset.

For example:

  1. List_for_each_entry (tmp, & mylist. list, list)
  2. Printf ("to = % d from = % d \ n", tmp-> to, tmp-> from );

This macro can be divided into two steps. The first step is the traversal chain table. pos points to the struct list_head structure of each item in the linked list in sequence, and the second step is to obtain the item where the struct list_head pointed by pos is located. Here is tmp.

List_for_each (pos, & mylist. list) {// records the linked list elements in sequence.

Tmp = list_entry (pos, struct kool_list, list); // obtain the data structure pointer containing the pos Node

5. Delete

To delete a node in a linked list, use secure traversal before deleting it.

For example:

  1. List_for_each_safe (pos, q, & mylist. list ){
  2. Tmp = list_entry (pos, struct kool_list, list );
  3. Printf ("freeing item to = % d from = % d \ n", tmp-> to, tmp-> from );
  4. List_del (pos );

6. Empty linked list

Int list_empty (struct list_head * head );

Returns a nonzero value if the given list is empty.


Reference: http://www.makelinux.net/ldd3/chp-11-sect-5

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.