C复杂链表

#include"string.h"
#include"stdio.h"
#include"stdlib.h"
typedef struct _ListElmt
{
	void * data;
	struct _ListElmt *next;
}ListElmt;
typedef struct _List
{
	int size;
	int (*match)(const void*  key1,const void* key2);
	void (*destory)(void * data);
	ListElmt *head;
	ListElmt *tail;
}List;
void List_init(List *list,void (*destory)(void * data));
void list_destory(List *list);
int  list_ins_next(List * list, ListElmt *element, const void * data);
int list_rem_next(List *list ,ListElmt *element ,void **data);
#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_data(element) ((element)->data)
#define list_next(element) ((element)->next)
void data_free(void * data)
{
	free(data);
}
int data_match(const void * key1 ,const void* key2)
{
	
}
void List_init(List *list,void (*destory)(void * data))
{
	list->size = 0;
	list->destory = destory;
	list->match = data_match;
	list->head = NULL;
	list->tail = NULL;
}
int list_ins_next(List * list, ListElmt *element, const void * data)
{
	/*判断分配空间是否成功,不成功为-1*/
	ListElmt * new_element =(ListElmt*)malloc(sizeof(ListElmt));
	if(new_element == NULL)
	{
		return -1;
	}
	new_element->data = (void*)data;
	/*如果element是null,那么插入到第一个位置*/
	if(element == NULL)
	{
		if(list_size(list) == 0)//如果是插入第一个元素的话, 那么设置尾指针 ; 
		list->tail = new_element;
		new_element->next = list->head;
		list->head = new_element;
	}
	else
	{
		if(element->next == NULL)//如果是插入到最后的话, 那么设置尾指针 ;
		list->tail = new_element;
		new_element->next = element->next;
		element->next = new_element;	
	}
	list->size++;
	return 0;
}
int list_rem_next(List *list ,ListElmt *element ,void **data)
{
	ListElmt * old_element ;
	
	if(list_size(list) == 0)
	{
		return -1;
	}
	/*如果element是null,那么删除第一个元素*/
	if(element == NULL)
	{
		*data = list->head->data;
		//printf("%s",*data);
		old_element =list->head;
		list->head = list->head->next;
		if(list_size(list) == 1)//如果是第一个元素,切唯一元素,那么尾指针设置NULL; 
		{
			list->tail = NULL;
		}
	} 
	else
	{
		if(element->next == NULL)//如果在最后一个元素后面再删除,发生错误; 
		return -1;
		*data = element->next->data;
		old_element = element->next;
		element->next = element->next->next;
		if(element->next->next == NULL)
		{
			list->tail = element->next;//删除最后一个元素; 
		}
	}
	list->size--;
	return 0;
}
void list_destory(List *list)
{
	void *data;
	while(list_size(list) > 0)
	{
		if(list_rem_next(list ,NULL ,(void **)&data) && list->destory != NULL)
		{
			list->destory(data);
		}
	}
	memset(list,0,sizeof(List));
}
int main()
{
	char * a = (char*)malloc(8*sizeof(char));
	char * b = (char*)malloc(8*sizeof(char));
	char * c = (char*)malloc(8*sizeof(char));
	sprintf(a,"%s","China");
	sprintf(b,"%s","Japan");
	sprintf(c,"%s","America");
	
	List * list =(List*)malloc(sizeof(List));
	List_init(list,data_free);
	
	list_ins_next(list,NULL,(void*)a);
	list_ins_next(list,NULL,(void*)b);
	list_ins_next(list,NULL,(void*)c);
	
	char * temp = NULL;
	list_rem_next(list,NULL,(void**)&temp);
	printf("%s\n",temp);
}

你可能感兴趣的:(C语言)