03dlist-kernel_list
来源:互联网 发布:ipad钢琴教学软件 编辑:程序博客网 时间:2024/04/30 03:51
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;
}
- 03dlist-kernel_list
- 03dlist-stack
- DList
- kernel_list讲解
- 02dlist-base
- 02dlist-priv
- DList双向链表
- General Dlist Implement by C
- zephyr学习笔记---双向链表dlist
- 20150203 【 内核链表 kernel_list.h 】 list_head 使用
- 【练习册】 2015-08-09 Dlist Queue Stack by python
- Linux 内核里的数据结构——双向链表(dlist)
- tag 'select', field 'list', name 'did': The requested list key '#dList' could not be resolved as a c
- 03
- 03
- 03
- 03
- 03
- ubuntu 安装git
- 每天一个linux命令(12):more命令
- currentStyle和getComputedStyle的兼容写法
- windows7利用EasyBCD安装centos6.3双系统
- 每天一个linux命令(13):less 命令
- 03dlist-kernel_list
- arp 相关
- mysql 备份 还原
- 每天一个linux命令(14):head 命令
- Android 判断SIM卡属于哪个移动运营商
- 复数类及各种操作(Java)
- 40款不容错过的个人摄影设计作品集网站
- Heat equation
- UIthread UI 线程