C
C++fan
The following code is for list operation. But I can not understand.
Could anyone explain the code for me?
/*
* List definitions.
*/
#define LIST_HEAD(name, type)
struct name {
type *lh_first; /* first element */
}
#define LIST_ENTRY(type)
struct {
type *le_next; /* next element */
type **le_prev; /* address of previous next element */
}
Questions:
Why the struct does not have a name?
Is the list a two-way list?
/*
* List functions.
*/
#define LIST_INIT(head) {
(head)->lh_first = NULL;
}
#define LIST_INSERT_AFTER(listelm, elm, field) {
if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)
(listelm)->field.le_next->field.le_prev =
&(elm)->field.le_next;
(listelm)->field.le_next = (elm);
(elm)->field.le_prev = &(listelm)->field.le_next;
}
Question:
What is listelm, elm, field?
#define LIST_INSERT_HEAD(head, elm, field) {
if (((elm)->field.le_next = (head)->lh_first) != NULL)
(head)->lh_first->field.le_prev =
&(elm)->field.le_next;
(head)->lh_first = (elm);
(elm)->field.le_prev = &(head)->lh_first;
}
Question:
If elm becomes head, what's the purpose of the last sentence:
(elm)->field.le_prev = &(head)->lh_first;
Thanks in advance.
Jack
Could anyone explain the code for me?
/*
* List definitions.
*/
#define LIST_HEAD(name, type)
struct name {
type *lh_first; /* first element */
}
#define LIST_ENTRY(type)
struct {
type *le_next; /* next element */
type **le_prev; /* address of previous next element */
}
Questions:
Why the struct does not have a name?
Is the list a two-way list?
/*
* List functions.
*/
#define LIST_INIT(head) {
(head)->lh_first = NULL;
}
#define LIST_INSERT_AFTER(listelm, elm, field) {
if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)
(listelm)->field.le_next->field.le_prev =
&(elm)->field.le_next;
(listelm)->field.le_next = (elm);
(elm)->field.le_prev = &(listelm)->field.le_next;
}
Question:
What is listelm, elm, field?
#define LIST_INSERT_HEAD(head, elm, field) {
if (((elm)->field.le_next = (head)->lh_first) != NULL)
(head)->lh_first->field.le_prev =
&(elm)->field.le_next;
(head)->lh_first = (elm);
(elm)->field.le_prev = &(head)->lh_first;
}
Question:
If elm becomes head, what's the purpose of the last sentence:
(elm)->field.le_prev = &(head)->lh_first;
Thanks in advance.
Jack