For example, the list of Linux cores (two)

Source: Internet
Author: User

Working with lists

I think the best way to become familiar with the core list feature is to look at some simple examples of material to better understand the chain list.

Here is a sample. Include Create. Join. Delete and traverse the linked list.

<span style= "FONT-SIZE:18PX;" > #include <stdio.h> #include <stdlib.h> #include "list.h" struct kool_list{int to;struct list_head list; int from;};/ /define the data amount structure to be linked, and include the doubly linked list structure int main (int argc, char **argv) {struct kool_list *tmp;struct list_head *pos, *q;unsigned int i;st Ruct kool_list Mylist;init_list_head (&mylist.list);//Initialize a list header/* to <span style= "font-family:arial, Helvetica, Sans-serif; " >mylist added element </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));//A new element node is added to the Necklace table, list/* in TMP can also use List_add_tail () which adds new items to * The tail end of the list */}printf ("\ n");/* now has 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. See "How to * does this work?"

"Below for a explanation of how" the This is done. */tmp= List_entry (pos, struct kool_list, list);//Get Data structures including 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>


Several Common Core inventory API implementations. The above case Linux core list (iii) is introduced.



For example, the list of Linux cores (two)

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.