Example of Linux kernel chain list (ii)

Source: Internet
Author: User
Tags prev

Linked list Use

I think the best way to get familiar with the kernel list is to look at a few simple examples, which is a very good material to better understand the list.

Here is an example that contains creating, adding, deleting, and traversing linked lists.

<span style= "FONT-SIZE:14PX;" ><span style= "color: #330099;" > #include <stdio.h> #include <stdlib.h> #include "list.h" struct kool_list{int to;struct list_head list; int from;};/ /Custom to link the data amount structure and contain the doubly linked list structure int main (int argc, char **argv) {struct kool_list *tmp;struct list_head *pos, *q;unsigned int i;str UCT kool_list Mylist;init_list_head (&mylist.list);//Initialize a list header/* to <span style= "font-family:arial, Helvetica, Sans-serif; " Add elements in >mylist </span><span style= "font-family:arial, Helvetica, Sans-serif;" > */</span>for (i=5; i!=0; i.) {tmp= (struct kool_list *) malloc (sizeof (struct kool_list));/* Init_list_head (  &tmp->list); * * This Initializes a dynamically allocated List_head.  We * can omit this if subsequent call was Add_list () or * anything along that line because the next, prev * Fields get Initialized in those functions. */printf ("Enter to and from:"), scanf ("%d%d", &tmp->to, &tmp->from);/* Add the new item ' TMP ' to the list OF Items in MyList */list_add (& (Tmp->list), & (Mylist.list));//Add new element node in the necklace table, TMP list/* You can also use List_ Add_tail () which adds new items to * the tail end of the list */}printf ("\ n");/* Now you have a circularly linked list of Items of type struct kool_list.  * Now let us go through the items and print them out *//* List_for_each () are a macro for a for loop. * First parameter is used as the counter on for loop. In other words, inside the * loop it points to the current item ' s list_head. * Second parameter is the pointer to the list. It is not a manipulated by the macro. */printf ("Traversing the list using List_for_each () \ n"); List_for_each (POS, &mylist.list) {//Traverse linked list, pos points to the element of the linked list in turn/* At this point:pos->next points to the next item ' s ' list ' variable and * pos->prev points to the previous item ' s ' L Ist ' variable. Here item is * of type struct kool_list. But we need to access the item itself not the * variable ' list ' in the item! Macro List_entry () does just that. SeeHow does * Does this work? "below for a explanation of how that is done. */tmp= List_entry (pos, struct kool_list, list);//Get data structure containing POS nodes <span style= "font-family:arial, Helvetica, Sans-serif; " >struct kool_list Pointer </span>/* Given a pointer to struct list_head, type of data structure it was part of, * and I T ' s name (struct List_head ' s name in the data structure) it returns a * pointer to the data structure in which the Pointe  R is part of.  * For example, on the above line List_entry () would return a pointer to the * struct Kool_list item It's embedded in! */printf ("to=%d from=%d\n", Tmp->to, Tmp->from);} printf ("\ n");/* Since this is a circularly linked list. You can traverse the list in reverse order * as well. All your need to do are replace ' list_for_each ' with ' list_for_each_prev ' * Everything else remain the same! * * Also You can traverse the list using List_for_each_entry () to iterate over a given * type of entries. For example: */printf ("Traversing the list usinG list_for_each_entry () \ n "); List_for_each_entry (TMP, &mylist.list, list) printf (" To=%d from=%d\n ", Tmp->to, Tmp->from);p rintf ("\ n");/* Now let's be good and free the kool_list items. Since we'll be removing items * off the list using List_del () We need to use a safer version of the List_for_each () * m Acro aptly named List_for_each_safe (). Note that you must use this macro if the loop * involves deletions of items (or moving items from one list to another). */printf ("Deleting the list using List_for_each_safe () \ n"); List_for_each_safe (POS, Q, &mylist.list) {tmp= list_ Entry (POS, struct kool_list, list); printf ("Freeing item to=%d from=%d\n", Tmp->to, Tmp->from); List_del (POS); Free (TMP);} return 0;} </span></span>
a few common API implementations of the kernel list are described in the example Linux kernel chain list (iii).


Example of Linux kernel chain list (ii)

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.