1. dlist.h
1 #ifndef _DLIST_H 2 #define _DLIST_H 3 4 5 typedef struct _dlist_node 6 { 7 struct _dlist_node *p_next; /* 指向下一个结点的指针 */ 8 struct _dlist_node *p_prev; /* 指向上一个结点的指针 */ 9 }dlist_node_t; 10 11 typedef dlist_node_t dlist_head_t; 12 typedef int (*dlist_node_process_t)(void *p_arg, dlist_node_t *p_node); 13 14 int dlist_init(dlist_head_t *p_head); 15 int dlist_add(dlist_head_t *p_head, dlist_node_t *p_pos, dlist_node_t *p_node); 16 int dlist_add_tail(dlist_head_t *p_head, dlist_node_t *p_node); 17 int dlist_add_head(dlist_head_t *p_head, dlist_node_t *p_node); 18 int dlist_del(dlist_head_t *p_head, dlist_node_t *p_node); 19 20 dlist_node_t *dlist_prev_get(dlist_head_t *p_head, dlist_node_t *p_pos); 21 dlist_node_t *dlist_next_get(dlist_head_t *p_head, dlist_node_t *p_pos); 22 dlist_node_t *dlist_tail_get(dlist_head_t *p_head); 23 dlist_node_t *dlist_begin_get(dlist_head_t *p_head); 24 dlist_node_t *dlist_end_get (dlist_head_t *p_head); 25 int dlist_foreach(dlist_head_t *p_head, 26 dlist_node_process_t pfn_node_process, 27 void *p_arg); 28 29 #endif
2. dlist.c
1 #include "dlist.h" 2 #include <stdlib.h> 3 4 int dlist_init(dlist_head_t *p_head) 5 { 6 if (p_head == NULL) 7 { 8 return -1; 9 } 10 p_head->p_next = p_head; 11 p_head->p_prev = p_head; 12 return 0; 13 } 14 15 int dlist_add(dlist_head_t *p_head, dlist_node_t *p_pos, dlist_node_t *p_node) 16 { 17 if ((p_head == NULL) || (p_pos == NULL) || (p_node == NULL)) 18 { 19 return -1; 20 } 21 p_node->p_prev = p_pos; 22 p_node->p_next = p_pos->p_next; 23 p_pos->p_next->p_prev = p_node; 24 p_pos->p_next = p_node; 25 return 0; 26 } 27 28 int dlist_add_tail(dlist_head_t *p_head, dlist_node_t *p_node) 29 { 30 return dlist_add(p_head, p_head->p_prev, p_node); 31 } 32 33 int dlist_add_head(dlist_head_t *p_head, dlist_node_t *p_node) 34 { 35 return dlist_add(p_head, p_head, p_node); 36 } 37 38 int dlist_del(dlist_head_t *p_head, dlist_node_t *p_node) 39 { 40 if ((p_head == NULL) || (p_node == NULL) || (p_node == p_head)) 41 { 42 return -1; 43 } 44 p_node->p_prev->p_next = p_node->p_next; 45 p_node->p_next->p_prev = p_node->p_prev; 46 p_node->p_next = NULL; 47 p_node->p_prev = NULL; 48 return 0; 49 } 50 51 dlist_node_t *dlist_prev_get(dlist_head_t *p_head, dlist_node_t *p_pos) 52 { 53 if (p_pos != NULL) 54 { 55 return p_pos->p_prev; 56 } 57 return NULL; 58 } 59 60 dlist_node_t *dlist_next_get(dlist_head_t *p_head, dlist_node_t *p_pos) 61 { 62 if (p_pos != NULL) 63 { 64 return p_pos->p_next; 65 } 66 return NULL; 67 } 68 69 dlist_node_t *dlist_tail_get(dlist_head_t *p_head) 70 { 71 if (p_head != NULL) 72 { 73 return p_head->p_prev; 74 } 75 return NULL; 76 } 77 78 dlist_node_t *dlist_begin_get(dlist_head_t *p_head) 79 { 80 if (p_head != NULL) 81 { 82 return p_head->p_next; 83 } 84 return NULL; 85 } 86 87 dlist_node_t *dlist_end_get (dlist_head_t *p_head) 88 { 89 return p_head; 90 /* 91 if (p_head != NULL) 92 { 93 return p_head->p_prev; 94 } 95 return NULL; 96 */ 97 } 98 99 int dlist_foreach(dlist_head_t *p_head, 100 dlist_node_process_t pfn_node_process, 101 void *p_arg) 102 { 103 dlist_node_t *p_tmp, *p_end; 104 int ret; 105 106 if ((p_head == NULL) || (pfn_node_process == NULL)) 107 { 108 return -1; 109 } 110 111 p_tmp = dlist_begin_get(p_head); 112 p_end = dlist_end_get(p_head); 113 114 while (p_tmp != p_end) 115 { 116 ret = pfn_node_process(p_arg, p_tmp); 117 if (ret < 0) 118 { 119 return ret; 120 } 121 p_tmp = dlist_next_get(p_head, p_tmp); 122 } 123 // pfn_node_process(p_arg, p_tmp); 124 return 0; 125 }
3. demo
1 #include <stdio.h> 2 #include "dlist.h" 3 4 typedef struct _dlist_int 5 { 6 dlist_node_t node; /* 包含链表结点 */ 7 int data; /* int类型数据 */ 8 }dlist_int_t; 9 10 int list_node_process(void *p_arg, dlist_node_t *p_node) 11 { 12 printf("%d ", ((dlist_int_t *)p_node)->data); 13 return 0; 14 } 15 16 int main(void) 17 { 18 dlist_head_t head; /* 定义链表头结点 */ 19 dlist_int_t node1, node2, node3; 20 dlist_init(&head); 21 22 node1.data = 1; 23 dlist_add(&head, &head, &(node1.node)); 24 node2.data = 2; 25 dlist_add(&head, &(node1.node), &(node2.node)); 26 node3.data = 3; 27 dlist_add(&head, &(node2.node), &(node3.node)); 28 printf("Before delete node. "); 29 dlist_foreach(&head, list_node_process, NULL); 30 dlist_del(&head, &(node2.node)); 31 printf(" After delet node. "); 32 dlist_foreach(&head, list_node_process, NULL); /* 遍历链表,用户参数为NULL */ 33 return 0; 34 }