DoublelistDemo(头 尾 中插入,头 尾删除,释放)

Makefile:

BIN     =  DoubleListDemo
CC = gcc

SRC =  DoubleList.c main.c 
OBJS = DoubleList.o main.o

CFLAGS  =  -Wall -O2

$(BIN): $(OBJS)
	$(CC) -o $(BIN) $(OBJS) $(CFLAGS)
$(OBJS): $(SRC) 
	gcc -c $(SRC) $(CFLAGS)


clean:
	rm -rf *.o
	rm -rf $(BIN)
distclean:
	-rm *.o 


 

DoubleList.h

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

typedef int KEY;

typedef struct {
	KEY key;
	int date;
}DATE;

typedef struct node{
	DATE date;
	struct node * pre;
	struct node * next;
}NODE;

typedef struct{
	int num;
	NODE *head;
	NODE *tail;
}DOUBLE_LIST;

/**********************************************/
void InitDoubleList(DOUBLE_LIST *list);

void DoubleListPrint(DOUBLE_LIST list);
void PrintNode(NODE * node);

void HeadInsterNode(DOUBLE_LIST *list, DATE date);
void XInsterNode(DOUBLE_LIST *list, DATE date, int x);
void TailInsterNode(DOUBLE_LIST *list, DATE date);

void HeadDeleteNode(DOUBLE_LIST *list);
void XDeleteNode(DOUBLE_LIST *list, KEY key);
void TailDeleteNode(DOUBLE_LIST *list);

void FreeList(DOUBLE_LIST *list);


 

DoubleList.c

#include "DoubleList.h"

/**********************************************************/
void InitDoubleList(DOUBLE_LIST *list)
{
	list->head = NULL;
	list->tail = NULL;
	list->num = 0;

	return ;
}
/**********************************************************/
void DoubleListPrint(DOUBLE_LIST list)
{
	int i = 0;
	NODE * p = NULL;

	p = list.head;
	
	if(0 == list.num){
		printf("Your List is Empty!\n");
		return;
	}
	
	for(i = 0; i < list.num; i++){
		PrintNode(p);
		p = p->next;
	}

	return;
}
/**********************************************************/
void PrintNode(NODE * node)
{
	printf("key:%d\tdate:%d\n",node->date.key,node->date.date);
	
	return;
}
/**********************************************************/
void HeadInsterNode(DOUBLE_LIST * list, DATE date)
{
	NODE * newnode = (NODE *)malloc(sizeof(NODE));

	newnode->date.key = date.key;
	newnode->date.date = date.date;

	if(0 == list->num){
		newnode->next = NULL;
		newnode->pre = NULL;
		list->head = newnode;
		list->tail = newnode;

		list->num++;

		return;
	}

	newnode->pre = NULL;
	newnode->next = list->head;
	list->head->pre = newnode;
	list->head = newnode;

	list->num++;

	return;
	
}
/**********************************************************/
void XInsterNode(DOUBLE_LIST *list, DATE date, int x)
{
	int i;
	NODE * newnode = NULL;
	NODE * prenode = NULL;
	NODE * nextnode = NULL;

	if(x < 0 ||x >= list->num){
		printf("X Inster: x is not right!\n");
		return;
	}

	if(0 == x){
		HeadInsterNode(list, date);
		return;
	}

	if(list->num == x - 1){
		TailInsterNode(list, date);
		return;
	}
	prenode = NULL;
	nextnode = list->head;

	for(i = 0; i < x; i++){
		prenode = nextnode;
		nextnode = nextnode->next;
	}
	
	newnode = (NODE *)malloc(sizeof(NODE));

	newnode->date.key = date.key;
	newnode->date.date = date.date;
	
	prenode->next = newnode;
	newnode->pre = prenode;
	newnode->next = nextnode;
	nextnode->pre = newnode;
	
	list->num++;
	
	return ;
}
/**********************************************************/
void TailInsterNode(DOUBLE_LIST *list, DATE date)
{
	NODE * newnode = (NODE *)malloc(sizeof(NODE));

	newnode->date.key = date.key;
	newnode->date.date = date.date;

	if(0 == list->num){
		newnode->next = NULL;
		newnode->pre = NULL;
		list->head = newnode;
		list->tail = newnode;

		list->num++;

		return;
	}

	newnode->next = NULL;
	newnode->pre = list->tail;
	list->tail->next = newnode;
	list->tail = newnode;

	list->num++;
	
	return;
}
/**********************************************************/
void HeadDeleteNode(DOUBLE_LIST *list)
{
	NODE * tmpnode = NULL;
	
	if(0 == list->num ){
		printf("%s:No Node In List!\n",__FUNCTION__);
		return;
	}
	if(1 == list->num){
		free(list->head);
		list->head = NULL;
		list->tail = NULL;
		list->num = 0;
		return ;
	}
	
	tmpnode = list->head;
	list->head = list->head->next;
	free(tmpnode);
	tmpnode = NULL;
	list->num--;

	return ;
}
/**********************************************************/
void XDeleteNode(DOUBLE_LIST *list, KEY key)
{
	;
}
/**********************************************************/
void TailDeleteNode(DOUBLE_LIST *list)
{
	NODE * tmpnode = NULL;
	
	if(0 == list->num ){
		printf("%s:No Node In List!\n",__FUNCTION__);
		return;
	}
	if(1 == list->num){
		free(list->head);
		list->head = NULL;
		list->tail = NULL;
		list->num = 0;
		return ;
	}
	
	tmpnode = list->tail;
	list->tail = list->tail->pre;
	list->tail->next = NULL;
	free(tmpnode);
	tmpnode = NULL;
	list->num--;
	
	return;
}
/**********************************************************/
void FreeList(DOUBLE_LIST *list)
{
	int i;
	NODE * tmpnode = NULL;
	int num;

	num = list->num;
	
	for(i = 0; i < num; i++){
		tmpnode = list->head;
		list->head = list->head->next;
		free(tmpnode);
		tmpnode = NULL;
		list->num--;
	}

	list->head = NULL;
	list->tail = NULL;

	return;
}


 

main.c

 

#include "DoubleList.h"

int main(int argc,char *argv[])
{
	DOUBLE_LIST list;

	int i;
	DATE date[10];

	for(i = 0; i < 10; i++){
		date[i].key = i + 1;
		date[i].date = i + 11;
	}
#if 1
	printf("init list test\n");
	InitDoubleList(&list);

	DoubleListPrint(list);
#endif
#if 1
        //Head inster test
        printf("head inster test\n");
	HeadInsterNode(&list, date[0]);
	HeadInsterNode(&list, date[1]);
	HeadInsterNode(&list, date[2]);
	HeadInsterNode(&list, date[3]);
	HeadInsterNode(&list, date[4]);

	DoubleListPrint(list);
#endif

#if 1
        //tail inster test
        printf("\ntail inster test\n");
	TailInsterNode(&list, date[0]);
	TailInsterNode(&list, date[1]);
	TailInsterNode(&list, date[2]);
	TailInsterNode(&list, date[3]);
	TailInsterNode(&list, date[4]);

	DoubleListPrint(list);
#endif

#if 1
	//x inster test
	printf("\nx inster test\n");
	XInsterNode(&list, date[5], 5);
	XInsterNode(&list, date[6], 6);
	XInsterNode(&list, date[7], 7);
	XInsterNode(&list, date[8], 8);
	XInsterNode(&list, date[9], 9);

	DoubleListPrint(list);
#endif

#if 1
	//head delete test
	printf("\nhead delete test\n");
        HeadDeleteNode(&list);
	HeadDeleteNode(&list);
	HeadDeleteNode(&list);
	HeadDeleteNode(&list);
	HeadDeleteNode(&list);
	
	DoubleListPrint(list);
#endif
#if 1
	//tail delete test
	printf("\ntail delete test\n");
	TailDeleteNode(&list);
	TailDeleteNode(&list);
	TailDeleteNode(&list);
	TailDeleteNode(&list);
	TailDeleteNode(&list);

	DoubleListPrint(list);
#endif

#if 1
	//free list test
	printf("\nfree list test\n");
	FreeList(&list);
	
	DoubleListPrint(list);
#endif
	return 0;
}


 

你可能感兴趣的:(DoublelistDemo(头 尾 中插入,头 尾删除,释放))