码迷,mamicode.com
首页 > 其他好文 > 详细

链表创建及增加节点

时间:2015-05-21 12:18:34      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

  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 [root@rac1 List]# gcc listcreate.c zmalloc.c
142 [root@rac1 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 */

 

链表创建及增加节点

标签:

原文地址:http://www.cnblogs.com/huanhuanang/p/4519066.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!