1 #include <stdio.h> 2 #include <string.h> 3 #include "zmalloc.h" 4 //定义节点 5 typedef struct listNode { 6 struct listNode *prev; 7 struct listNode *next; 8 void *value; 9 } listNode; 10 11 //定义链表 12 typedef struct list { 13 listNode *head; 14 listNode *tail; 15 void *(*dup)(void *ptr); 16 void (*free)(void *ptr); 17 int (*match)(void *ptr, void *key); 18 unsigned long len; 19 } list; 20 21 //创建一个空链表 22 list *listCreate(void) 23 { 24 struct list *list; 25 26 if ((list = zmalloc(sizeof(*list))) == NULL) 27 return NULL; 28 list->head = list->tail = NULL; 29 list->len = 0; 30 list->dup = NULL; 31 list->free = NULL; 32 list->match = NULL; 33 return list; 34 } 35 36 /* Add a new node to the list, to head, containing the specified ‘value‘ 37 * pointer as value. 38 * 39 * On error, NULL is returned and no operation is performed (i.e. the 40 * list remains unaltered). 41 * On success the ‘list‘ pointer you pass to the function is returned. */ 42 list *listAddNodeHead(list *list, void *value) 43 { 44 listNode *node; 45 46 if ((node = zmalloc(sizeof(*node))) == NULL) 47 return NULL; 48 node->value = value; 49 if (list->len == 0) { 50 list->head = list->tail = node; 51 node->prev = node->next = NULL; 52 } else { 53 node->prev = NULL; 54 node->next = list->head; 55 list->head->prev = node; 56 list->head = node; 57 } 58 list->len++; 59 return list; 60 } 61 62 /* Add a new node to the list, to tail, containing the specified ‘value‘ 63 * pointer as value. 64 * 65 * On error, NULL is returned and no operation is performed (i.e. the 66 * list remains unaltered). 67 * On success the ‘list‘ pointer you pass to the function is returned. */ 68 list *listAddNodeTail(list *list, void *value) 69 { 70 listNode *node; 71 72 if ((node = zmalloc(sizeof(*node))) == NULL) 73 return NULL; 74 node->value = value; 75 if (list->len == 0) { 76 list->head = list->tail = node; 77 node->prev = node->next = NULL; 78 } else { 79 node->prev = list->tail; 80 node->next = NULL; 81 list->tail->next = node; 82 list->tail = node; 83 } 84 list->len++; 85 return list; 86 } 87 88 /* Free the whole list. 89 * 90 * This function can‘t fail. */ 91 void listRelease(list *list) 92 { 93 unsigned long len; 94 listNode *current, *next; 95 96 current = list->head; 97 len = list->len; 98 while(len--) { 99 next = current->next; 100 if (list->free) list->free(current->value); 101 zfree(current); 102 current = next; 103 } 104 zfree(list); 105 } 106 107 int main() 108 { 109 list *keys = listCreate(); 110 111 char buf[1024]; 112 strcpy(buf,"i am zhaoja"); 113 char buf2[1024]; 114 strcpy(buf2,"i am xm"); 115 116 listAddNodeHead(keys,buf); 117 listAddNodeHead(keys,buf2); 118 119 printf("keys head is %s\n",keys->head->value); 120 printf("keys tail is %s\n",keys->tail->value); 121 printf("keys len is %d\n",keys->len); 122 123 char buf3[1024]; 124 strcpy(buf3,"i am x3"); 125 char buf4[1024]; 126 strcpy(buf4,"i am x4"); 127 128 listAddNodeTail(keys,buf3); 129 listAddNodeTail(keys,buf4); 130 131 printf("keys head is %s\n",keys->head->value); 132 printf("keys tail is %s\n",keys->tail->value); 133 printf("keys len is %d\n",keys->len); 134 135 listRelease(keys); 136 return 0; 137 } 138 139 /* 140 测试代码 141 [[email protected] List]# gcc listcreate.c zmalloc.c 142 [[email protected] List]# ./a.out 143 keys head is i am xm 144 keys tail is i am zhaoja 145 keys len is 2 146 keys head is i am xm 147 keys tail is i am x4 148 keys len is 4 149 150 */
时间: 2024-11-13 05:16:15