#define LIST_CONCAT2(s1, s2) s1##s2 #define LIST_CONCAT(s1, s2) LIST_CONCAT2(s1, s2)
该宏的作用非常简单,将s1,s2拼接在一起。其中,##起连接作用,编译器在预处理时负责将s1,s2连接在一起。比如hello_##world经过编译器预处理后就变成了hello_world了。
typedef void ** list_t; #define LIST(name) \ static void *LIST_CONCAT(name,_list) = NULL; \ static list_t name = (list_t)&LIST_CONCAT(name,_list)
经过宏替换后,变为
#define LIST(name) \ static void *name_list = NULL; \ static list_t name = (list_t)&name_list
该宏用于申明一个链表。可以将name_list理解为链表头(且该表头元素是一个指针),将name理解为一个指向链表头的指针。需要注意的是,name_list所指向的类型必须是一个结构体。
#define LIST_STRUCT(name) \ void *LIST_CONCAT(name,_list); \ list_t name
#define LIST_STRUCT_INIT(struct_ptr, name) \ do { \ (struct_ptr)->name = &((struct_ptr)->LIST_CONCAT(name,_list)); \ (struct_ptr)->LIST_CONCAT(name,_list) = NULL; \ list_init((struct_ptr)->name); \ } while(0)
struct list { struct list *next; };
struct list只有一个指针成员next,它指向的类型依然是struct list。在Contiki中,该结构体用于用于遍历链表,我们将在后面的链表函数中看到如何使用该结构体。
void list_init(list_t list) { *list = NULL; }
作用:初始化链表
解释:
void * list_head(list_t list) { return *list; }
作用:获取链表头
解释:
void
list_copy(list_t dest, list_t src)
{
*dest = *src; }
作用:“拷贝”链表
解释:
void * list_tail(list_t list) { struct list *l; if(*list == NULL) { return NULL; } for(l = *list; l->next != NULL; l = l->next); return l; }
作用:返回链表尾部节点
解释:
void
list_add(list_t list, void *item) { struct list *l; /* Make sure not to add the same element twice */ list_remove(list, item); ((struct list *)item)->next = NULL; l = list_tail(list); if(l == NULL) { *list = item; } else { l->next = item; } }
作用:在链表尾部加入节点
解释:
void
list_push(list_t list, void *item) { /* struct list *l;*/ /* Make sure not to add the same element twice */ list_remove(list, item); ((struct list *)item)->next = *list; *list = item; }
作用:在链表头部压入节点
解释:
void * list_chop(list_t list) { struct list *l, *r; if(*list == NULL) { return NULL; } if(((struct list *)*list)->next == NULL) { l = *list; *list = NULL; return l; } for(l = *list; l->next->next != NULL; l = l->next); r = l->next; l->next = NULL; return r; }
作用:砍掉链表尾部节点,并返回该尾部节点
解释:
void * list_pop(list_t list) { struct list *l; l = *list; if(*list != NULL) { *list = ((struct list *)*list)->next; } return l; }
作用:弹出链表头结点
解释:
void list_remove(list_t list, void *item) { struct list *l, *r; if(*list == NULL) { return; } r = NULL; for(l = *list; l != NULL; l = l->next) { if(l == item) { if(r == NULL) { /* First on list */ *list = l->next; } else { /* Not first on list */ r->next = l->next; } l->next = NULL; return; } r = l; } }
作用:删除item所指向的节点
解释:
int list_length(list_t list) { struct list *l; int n = 0; for(l = *list; l != NULL; l = l->next) { ++n; } return n; }
作用:返回链表长度
解释:
void list_insert(list_t list, void *previtem, void *newitem) { if(previtem == NULL) { list_push(list, newitem); } else { ((struct list *)newitem)->next = ((struct list *)previtem)->next; ((struct list *)previtem)->next = newitem; } }
作用:插入链表节点
解释:
void * list_item_next(void *item) { return item == NULL? NULL: ((struct list *)item)->next; }
作用:返回链表中item的下一个节点