0
votes

I'm trying to use the list utility that is recommended in Linux kernel. From the introduction in https://isis.poly.edu/kulesh/stuff/src/klist/ one of the feature of the list_head is that the element can belongs to different lists.

Now, if I want a list of all the staff and a list of the old staff only, I think I have to define the following data and structure:

struct list_head all_staff;
struct list_head old_staff;

struct staff {
    int age;
    struct list_head list;    // for all the staff;
    struct list_head old;
};

On the other hand, the normal list implementation may be like this:

struct staff {
    int age;
};

struct node {
    struct node *prev;
    struct node *next;
    void *element;
};

What's the advantage of list_head compare to the normal list implementation? I feel although the list_head only includes 2 points, removes the point to the element, and so it saves memory, but in this case, I have to add another list_head in the staff structure, if there is only 1 old staff, then it will waste lots of memory.

What's more, if the staff belongs to many different groups, does it mean I have to add many list_head in the staff structure?

Thanks.

1
Depends how you are going to use it it might make sense to go iterate over the list instead of managing several lists. - 0andriy
Link is dead. Please fix it. - 0andriy

1 Answers

1
votes

if the staff belongs to many different groups, does it mean I have to add many list_head in the staff structure?

Yes, you need several list_head fields in the stuff object so it can belong to several lists at the same time.

What's the advantage of list_head compare to the normal list implementation?

list_head is ready-made implementation. But when use "normal" lists, you need to implement list functions manually for every object type you use as a list element.