Chinaunix首页 | 论坛 | 博客
  • 博客访问: 425750
  • 博文数量: 115
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 393
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-26 12:10
个人简介

踏实做事,认真做人

文章分类

全部博文(115)

文章存档

2017年(1)

2016年(2)

2015年(14)

2014年(63)

2013年(35)

分类: C/C++

2013-06-20 11:44:38


点击(此处)折叠或打开

  1. #ifndef __LIST_H
  2. #define __LIST_H

  3. /* data structure element abstract */
  4. typedef struct ListElmt_t {
  5.     void *data;
  6.     struct ListElmt_t *next;
  7. }ListElmt;

  8. typedef struct List_t {
  9.     int            size;
  10.     int            (*match)(const void *key1, const void *key2);
  11.     void        (*destroy)(void *data);
  12.     ListElmt *head;
  13.     ListElmt    *tail;
  14. }List;

  15. /* Public Interface */
  16. void list_init(List *list, void (*destroy)(void *data));
  17. int list_insert_next(List *list, ListElmt *element, const void *data);
  18. int list_rem_next(List *list, ListElmt *element, void **data);
  19. int list_destroy(List *list);
  20. int list_reverse(List *list);
  21. void list_print(List *list);

  22. /* Macro */
  23. #define list_size(list)    ((list)->size)
  24. #define list_head(list)    ((list)->head)
  25. #define list_tail(list)    ((list)->tail)

  26. #define list_is_head(list, element)    ((element) == (list)->head ? 1 : 0)
  27. #define list_is_tail(element)    ((element)->next == NULL ? 1 : 0)
  28. #define list_data(element) ((element)->data)
  29. #define list_next(element) ((element)->next)

  30. #endif


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #include "list.h"

  4. void list_init(List *list, void (*destroy)(void *data))
  5. {
  6.     list->size = 0;
  7.     list->destroy = destroy;
  8.     list->head = NULL;
  9.     list->tail = NULL;
  10. }

  11. int list_insert_next(List *list, ListElmt *element, const void *data)
  12. {
  13.     ListElmt *new_element;

  14.     if ((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL)
  15.     {
  16.         return -1;
  17.     }

  18.     new_element->data = (void *)data;

  19.     if (NULL == element)
  20.     {
  21.         /* Handle insert at the head of list */
  22.         if (list_size(list) == 0)
  23.             list->tail = new_element;

  24.         new_element->next = list->head;
  25.         list->head = new_element;

  26.     }else
  27.     {
  28.         /* Handle insert somewhere other than at the head */
  29.         if (element->next == NULL)
  30.             list->tail = new_element;

  31.         new_element->next = element->next;
  32.         element->next = new_element;

  33.     }

  34.     list->size++;

  35.     return 0;
  36. }

  37. int list_reverse(List *list)
  38. {
  39.     ListElmt *element_old;
  40.     ListElmt *element_new;

  41.     element_old = list->head;

  42.     list->head = NULL;/* Tail node */

  43.     while (element_old != NULL)
  44.     {
  45.         element_new = element_old;
  46.         element_old = element_old->next;
  47.         element_new->next = list->head;
  48.         list->head = element_new;
  49.     }

  50.     return 0;
  51. }

  52. void list_print(List *list)
  53. {    
  54.     ListElmt *element;
  55.     int         *data;
  56.     int i = 1;

  57.     element = list_head(list);

  58.     while (element != NULL)    
  59.     {

  60.         data = list_data(element);

  61.         printf("ListElement[%d] = %d\n", i, *data);

  62.         i++;
  63.         element = element->next;
  64.     }
  65. }

main.c


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "list.h"

  4. int main(int argc, char *argv[])
  5. {
  6.     List list;
  7.     int *data;
  8.     int i;
  9.     int ret;
  10.     int b = 5;

  11.     list_init(&list, free);

  12.     for(i =100; i > 0; i--)
  13.     {
  14.         if ( (data = (int *)malloc(sizeof(int))) == NULL)
  15.             return -1;

  16.         *data = i;
  17.         list_insert_next(&list, NULL, data);
  18.     }

  19.     list_print(&list);

  20.     ret = list_reverse(&list);
  21.     if (ret != 0)
  22.     {
  23.         printf("reverse fail.");
  24.         return -1;
  25.     }
  26.     
  27.     list_print(&list);

  28.     return 0;
  29. }

Makefile

点击(此处)折叠或打开

  1. OBJ=$(patsubst %.c,%.o,$(wildcard *.c))
  2. BIN=list_example
  3. CFLAGS=-g -Wall -O0

  4. $(BIN):$(OBJ)
  5.     $(CC) -o $@ $(LFLAGS) $^

  6. .c.o:
  7.     $(CC) -c -o $@ $< $(CFLAGS)

  8. clean:
  9.     @rm -rf *.o $(BIN)

  10. .PHONY: clean

Notes:
1)list data memory havent free,caused memory leak,need to update programm later
阅读(1206) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~