#ifndef _LIST_H_
#define _LIST_H_
#include <stdio.h>
typedef int status;
#define ret_success 0
#define ret_fail -1
#define ret_exist 1
#define ret_not_exit 2
typedef struct Node_
{
void *data;
struct Node_ *next;
}Node;
typedef struct List_
{
int size;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
Node *head;
Node *tail;
}List;
#define list_size(list) ((list)->size)
#define list_head(list) ((list)->head)
#define list_tail(list) ((list)->tail)
#define list_is_head(list, Node) ((Node) == (list)->head ? 1 : 0)
#define list_is_tail(list, Node) ((Node)->next == NULL ? 1 : 0 )
#define list_data(Node) ((Node)->data)
#define list_next(Node) ((Node)->next)
void list_init(List *list, void(*destroy)(void *data));
void list_insert_next(List *list, Node *node, const void *data);
void list_remove_next(List *list, Node *node, void *data);
void list_destroy(List *list);
status list_insert(List *list, const void *data);
status list_remove(List *list, void *data);
void list_reverse(List * list);
//void List2Array(List* list, void *array);
#endif
#include <string.h>
#include <stdlib.h>
#include "List.h"
int (*str_cmp)(const void* data1, const void* data2);
int str_cmp_string(const char* data1, const char* data2)
{
return strcmp(data1, data2);
}
int str_cmp_int(const int* data1, const int* data2)
{
return (*data1 - *data2);
}
void list_init(List *list, void(*destroy)(void *data))
{
list->size = 0;
list->head = NULL;
list->tail = NULL;
list->destroy = destroy;//析构函数
return;
}
/* 将一个元素data插入结点node之后 */
void list_insert_next(List *list, Node *node, const void *data)
{
Node *new_node = (Node *)malloc(sizeof(Node));
if(new_node == NULL)
{
exit(-1);
}
new_node->data = (void *)data;
if(node == NULL) //结点为尾结点
{
if(list_size(list) == 0) //只有一个头结点
{
list->tail = new_node;
}
new_node->next = list->head;//好!形成一个只有头节点
list->head = new_node;
}
else
{
if(node->next == NULL)
{
list->tail = node;
}
new_node->next = node->next;
node->next = new_node;
}
list->size++;
return;
}
void list_remove_next(List *list, Node *node, void *data)
{
Node *old_node;
if(list_size(list) == 0)
{
exit(-1);
}
/*删除头*/
if(node == NULL)
{
data = list->head->data;
old_node = list->head;
list->head = list->head->next;
if(list_size(list) == 1)
{
list->tail == NULL;
}
}
else
{
if(node->next == NULL)
{
exit(-1);
}
data = node->next->data;
old_node = node->next;
node->next = node->next->next;
if(node->next == NULL)
{
list->tail = node;
}
}
free(old_node);
list->size--;
return;
}
void list_destroy(List * list)
{
/*指向被销毁的列表元素*/
void *data;
while(list_size(list) > 0)
{
/*每次都删除头*/
list_remove_next(list, NULL, data);
if(list->destroy != NULL)
{
/*如何处理被删除的函数由调用者传递的析构函数决定*/
list->destroy(data);
}
}
/*清空链表结构*/
memset(list, 0, sizeof(List));
return;
}
//升序排列
status list_insert(List* list, const void *data)
{
Node* new_node;
Node* pre_node = list->head;
Node* iter = list->head;
while(iter != NULL)
{
if(str_cmp((iter->data), data) > 0)
{
list_insert_next(list, pre_node, data);
if (iter == list->head)
{
iter->next->data = list->head->data;
list->head->data = (void *)data;
}
return ret_success;
}
else if(str_cmp((iter->data), data) == 0)
{
return ret_exist;
}
pre_node = iter;
iter = iter->next;
}
list_insert_next(list, pre_node, data);
return ret_success;
}
status list_remove(List *list, void *data)
{
Node* pre_node = list->head;
Node* iter = list->head;
while(iter != NULL)
{
if(str_cmp((iter->data), data) == 0)
{
if (iter == list->head)
{
if (iter->next != NULL)
{
list->head->data = iter->next->data;
iter->next->data = (void *)data;
}
else
{
pre_node = NULL;
}
}
list_remove_next(list, pre_node, data);
return ret_success;
}
pre_node = iter;
iter = iter->next;
}
return ret_not_exit;
}
void list_reverse(List* list)
{
Node* iter = list->head;
while(iter != NULL)
{
printf("[%d]\n", *(int *)(iter->data));
iter = iter->next;
}
}
int main()
{
List* list = (List *)malloc(sizeof(List));
int a = 2, b = 3, c = 6, d = 4, e = 5, f = 1;
str_cmp = &str_cmp_int;
list_init(list, NULL);
list_insert(list, &a);
list_insert(list, &b);
list_insert(list, &c);
list_insert(list, &d);
list_insert(list, &e);
list_insert(list, &f);
list_reverse(list);
list_remove(list, &f);
list_remove(list, &a);
list_remove(list, &b);
list_remove(list, &d);
list_remove(list, &e);
list_remove(list, &c);
list_reverse(list);
list_destroy(list);
list_init(list, NULL);
list_insert(list, &a);
list_insert(list, &b);
list_insert(list, &c);
list_insert(list, &d);
list_insert(list, &e);
list_insert(list, &f);
list_reverse(list);
list_remove(list, &f);
list_remove(list, &a);
list_remove(list, &b);
list_remove(list, &d);
list_remove(list, &e);
list_remove(list, &c);
list_reverse(list);
list_destroy(list);
return 0;
}