Why does the doubly linked list in sys/queue.h maintain the address of previous next element?

后端 未结 2 1660
无人及你
无人及你 2020-12-14 13:29

I\'m studying sys/queue.h from FreeBSD and I have one question:

In sys/queue.h, LIST_ENTRY is defined as follows:

#define LIST_ENTRY(typ         


        
2条回答
  •  不思量自难忘°
    2020-12-14 13:49

    Let me try to explain. Actually the **le_prev* affords ablity to list defined by sys/queue.h to insert_before that forward-list can not. Compared with insert_before, the insert_after can both be implemented well in forward-list or list. So list is more functional.

    insert_before(entry* list_elem, entry* elem, type val)
    {
        elem->next = list_elem;
        *(list->prev) = elem;
        elem->prev = *(list->prev);
        list_elem->prev = elem->next;
    }
    insert_after(entry* list_elem, entry* elem, type val)
    {
        if( ((elem)->next= (list_elem)->next) != NULL ) {
            (elem_list)->next->prev = &(elem)->next;
        }
        (list_elem)->next =  elem;
        elem->prev =  &(list_elem)->next;
    }
    

提交回复
热议问题