-
#ifndef __LIST_H
-
#define __LIST_H
-
-
/* data structure element abstract */
-
typedef struct ListElmt_t {
-
void *data;
-
struct ListElmt_t *next;
-
}ListElmt;
-
-
typedef struct List_t {
-
int size;
-
int (*match)(const void *key1, const void *key2);
-
void (*destroy)(void *data);
-
ListElmt *head;
-
ListElmt *tail;
-
}List;
-
-
/* Public Interface */
-
void list_init(List *list, void (*destroy)(void *data));
-
int list_insert_next(List *list, ListElmt *element, const void *data);
-
int list_rem_next(List *list, ListElmt *element, void **data);
-
int list_destroy(List *list);
-
int list_reverse(List *list);
-
void list_print(List *list);
-
-
/* Macro */
-
#define list_size(list) ((list)->size)
-
#define list_head(list) ((list)->head)
-
#define list_tail(list) ((list)->tail)
-
-
#define list_is_head(list, element) ((element) == (list)->head ? 1 : 0)
-
#define list_is_tail(element) ((element)->next == NULL ? 1 : 0)
-
#define list_data(element) ((element)->data)
-
#define list_next(element) ((element)->next)
-
-
#endif
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
#include "list.h"
-
-
void list_init(List *list, void (*destroy)(void *data))
-
{
-
list->size = 0;
-
list->destroy = destroy;
-
list->head = NULL;
-
list->tail = NULL;
-
}
-
-
int list_insert_next(List *list, ListElmt *element, const void *data)
-
{
-
ListElmt *new_element;
-
-
if ((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL)
-
{
-
return -1;
-
}
-
-
new_element->data = (void *)data;
-
-
if (NULL == element)
-
{
-
/* Handle insert at the head of list */
-
if (list_size(list) == 0)
-
list->tail = new_element;
-
-
new_element->next = list->head;
-
list->head = new_element;
-
-
}else
-
{
-
/* Handle insert somewhere other than at the head */
-
if (element->next == NULL)
-
list->tail = new_element;
-
-
new_element->next = element->next;
-
element->next = new_element;
-
-
}
-
-
list->size++;
-
-
return 0;
-
}
-
-
int list_reverse(List *list)
-
{
-
ListElmt *element_old;
-
ListElmt *element_new;
-
-
element_old = list->head;
-
-
list->head = NULL;/* Tail node */
-
-
while (element_old != NULL)
-
{
-
element_new = element_old;
-
element_old = element_old->next;
-
element_new->next = list->head;
-
list->head = element_new;
-
}
-
-
return 0;
-
}
-
-
void list_print(List *list)
-
{
-
ListElmt *element;
-
int *data;
-
int i = 1;
-
-
element = list_head(list);
-
-
while (element != NULL)
-
{
-
-
data = list_data(element);
-
-
printf("ListElement[%d] = %d\n", i, *data);
-
-
i++;
-
element = element->next;
-
}
-
}
main.c
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include "list.h"
-
-
int main(int argc, char *argv[])
-
{
-
List list;
-
int *data;
-
int i;
-
int ret;
-
int b = 5;
-
-
list_init(&list, free);
-
-
for(i =100; i > 0; i--)
-
{
-
if ( (data = (int *)malloc(sizeof(int))) == NULL)
-
return -1;
-
-
*data = i;
-
list_insert_next(&list, NULL, data);
-
}
-
-
list_print(&list);
-
-
ret = list_reverse(&list);
-
if (ret != 0)
-
{
-
printf("reverse fail.");
-
return -1;
-
}
-
-
list_print(&list);
-
-
return 0;
-
}
Makefile
-
OBJ=$(patsubst %.c,%.o,$(wildcard *.c))
-
BIN=list_example
-
CFLAGS=-g -Wall -O0
-
-
$(BIN):$(OBJ)
-
$(CC) -o $@ $(LFLAGS) $^
-
-
.c.o:
-
$(CC) -c -o $@ $< $(CFLAGS)
-
-
clean:
-
@rm -rf *.o $(BIN)
-
-
.PHONY: clean
Notes:
1)list data memory havent free,caused memory leak,need to update programm later
阅读(1197) | 评论(0) | 转发(0) |