- 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>
- #include <string.h>
- typedef int KEY;
- typedef struct {
- KEY key;
- int date;
- }DATE;
- typedef structnode{
- DATE date;
- struct node *pre;
- struct node *next;
- }NODE;
- typedef struct{
- int num;
- NODE *head;
- NODE *tail;
- }DOUBLE_LIST;
- voidInitDoubleList(DOUBLE_LIST *list);
- voidDoubleListPrint(DOUBLE_LIST list);
- void PrintNode(NODE *node);
- voidHeadInsterNode(DOUBLE_LIST *list, DATEdate);
- void XInsterNode(DOUBLE_LIST*list, DATE date, int x);
- voidTailInsterNode(DOUBLE_LIST *list, DATEdate);
- voidHeadDeleteNode(DOUBLE_LIST *list);
- void XDeleteNode(DOUBLE_LIST*list, KEY key);
- voidTailDeleteNode(DOUBLE_LIST *list);
- void FreeList(DOUBLE_LIST*list);
DoubleList.c
- #include "DoubleList.h"
- voidInitDoubleList(DOUBLE_LIST *list)
- {
- list->head = NULL;
- list->tail = NULL;
- list->num = 0;
- return ;
- }
- voidDoubleListPrint(DOUBLE_LIST list)
- {
- int i =0;
- NODE * p = NULL;
- p = list.head;
- if(0 ==list.num){
- printf("Your List isEmpty!\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;
- }
- voidHeadInsterNode(DOUBLE_LIST * list, DATEdate)
- {
- 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 notright!\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 ;
- }
- voidTailInsterNode(DOUBLE_LIST *list, DATEdate)
- {
- 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;
- }
- voidHeadDeleteNode(DOUBLE_LIST *list)
- {
- NODE * tmpnode = NULL;
- if(0 == list->num){
- printf("%s:NoNode InList!\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)
- {
- ;
- }
- voidTailDeleteNode(DOUBLE_LIST *list)
- {
- NODE * tmpnode = NULL;
- if(0 == list->num){
- printf("%s:NoNode InList!\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;
- }
- #if1
- printf("init listtest\n");
- InitDoubleList(&list);
- DoubleListPrint(list);
- #endif
- #if1
- //Head inster test
- printf("headinster 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
- #if1
- //tail inster test
- printf("\ntailinster 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
- #if1
- //x inster test
- printf("\nxinster 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
- #if1
- //head delete test
- printf("\nheaddelete test\n");
- HeadDeleteNode(&list);
- HeadDeleteNode(&list);
- HeadDeleteNode(&list);
- HeadDeleteNode(&list);
- HeadDeleteNode(&list);
- DoubleListPrint(list);
- #endif
- #if 1
- //tail deletetest
- printf("\ntail deletetest\n");
- TailDeleteNode(&list);
- TailDeleteNode(&list);
- TailDeleteNode(&list);
- TailDeleteNode(&list);
- TailDeleteNode(&list);
- DoubleListPrint(list);
- #endif
- #if 1
- //free listtest
- printf("\nfree listtest\n");
- FreeList(&list);
- DoubleListPrint(list);
- #endif
- return 0;
- }