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

2014-11-24 02:37:36 · 作者: · 浏览: 5
renode = 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
[cpp]
#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 ins