[cpp]
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
[cpp]
#include
#include
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
[cpp]
#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;
}
p