list.h
struct list_head {
struct list_head *prev;
struct list_head *next;
};
#define LIST_HEAD(head) struct list_head head = {&head, &head}
static inline void INIT_LIST_HEAD(struct list_head *node)
{
node->prev = node;
node->next = node;
}
static inline void __list_add(struct list_head *node,
struct list_head *prev,
struct list_head *next)
{
node->prev = prev;
node->next = next;
prev->next = node;
next->prev = node;
}
static inline void list_add(struct list_head *node,
struct list_head *head)
{
__list_add(node, head, head->next);
}
static inline void list_add_tail(struct list_head *node,
struct list_head *head)
{
__list_add(node, head->prev, head);
}
static inline int list_emtpy(struct list_head *head)
{
return head->next == head;
}
static inline void list_del(struct list_head *node)
{
node->prev->next = node->next;
node->next->prev = node->prev;
}
static inline void list_del_init(struct list_head *node)
{
list_del(node);
INIT_LIST_HEAD(node);
}
#define offsetof(type, member) \
((size_t)(&((type*)0)->member))
#define container_of(ptr, type, member) \
((type*)((char*)ptr - offsetof(type, member)))
/* @cur: ?..list_head?..?.复?舵.?
* @head: 澶磋.?圭.?板.
*/
#define list_for_each(cur, head) \
for (cur = (head)->next; \
(cur) != (head); \
cur = (cur)->next)
#define list_for_each_safe(cur, tmp, head) \
for (cur = (head)->next, tmp = (cur)->next; \
(cur) != (head); \
cur = tmp, tmp = (tmp)->next)
#define list_for_each_entry(ptr, head, member) \
for ( ptr = container_of((head)->next, typeof(*(ptr)), member); \
&(ptr)->member != (head); \
ptr = container_of((ptr)->member.next, typeof(*(ptr)), member) )
#define list_for_each_entry_safe(ptr, tmp, head, member) \
for ( ptr = container_of((head)->next, typeof(*(ptr)), member); \
(&(ptr)->member != (head)) && (tmp = container_of((ptr)->member.next, typeof(*(ptr)), member)); \
ptr = tmp )
#define list_for_each_continue(cur, head) \
for (cur = (cur)->next; \
(cur) != (head); \
cur = (cur)->next)
#define list_for_each_reverse(cur, head) \
for (cur = (head)->prev; \
(cur) != (head); \
cur = (cur)->prev)
test.c
#include <stdio.h>
#include <string.h>
#include "list.h"
struct node {
const char *name;
size_t data;
struct list_head list;
};
int main()
{
LIST_HEAD(list);
size_t i = 0;
struct node s[] = {
{"jack", 10},
{"mike", 20},
{"mary", 12},
{"tom", 15},
{"peter", 17},
{"kate", 18},
};
for (i = 0; i < sizeof(s) / sizeof(struct node); i++) {
list_add_tail(&s[i].list, &list);
}
struct list_head *cur = NULL;
struct list_head *tmp = NULL;
struct node *pnode = NULL;
struct node *ptmp = NULL;
list_for_each_entry(pnode, &list, list) {
printf("%s: %ld\n", pnode->name, pnode->data);
}
printf("\n");
/* list_for_each_safe(cur, tmp, &list) {
* pnode = container_of(cur, struct node, list);
* if (!strcmp(pnode->name, "tom")) {
* list_del_init(cur);
* }
* }*/
list_for_each_entry_safe(pnode, ptmp, &list, list) {
if (!strcmp(pnode->name, "mary")) {
list_del_init(&pnode->list);
}
}
list_for_each(cur, &list) {
pnode = container_of(cur, struct node, list);
printf("%s: %ld\n", pnode->name, pnode->data);
}
return 0;
}