/******************************************************
*linux gcc下测试通过 (有Bug请提交)
*使用本代码需要注名作者: fqheda
*本代码遵循GPL V3.0标准,可免费使用 -- 双向链表的代码实现
******************************************************/
/* dlink_list.c - by fqheda */
#include <stdio.h>
#include <stdlib.h>
typedef struct person_id
{
int num;
char name[16];
short sex;
}ElemType;
typedef struct dllnode
{
ElemType data;
struct dllnode *prev;
struct dllnode *next;
}DLinkList;
/*********************************************************
* Create Double Link List (With Head Node)
*********************************************************/
DLinkList *DLListCreate(ElemType x)
{
DLinkList *dq;
dq = (DLinkList *)malloc(sizeof(DLinkList));
if(dq == NULL)
{
printf("ERROR:: DLListCreate()->malloc() (not enough memory)!/n");
return NULL;
}
memcpy(&(dq->data), &x, sizeof(ElemType));
dq->prev = NULL;
dq->next = NULL;
return dq;
}
/**********************************************************
* Insert Double Link List
**********************************************************/
int DLListInsert(DLinkList *dq, int elem_num, ElemType x)
{
DLinkList *p;
DLinkList *s;
int iloop = 0;
if(dq == NULL)
{
printf("ERROR:: DLListInsert() pointer(pararm 1) can't be NULL!/n");
return -1;
}
p = dq;
while((p!= NULL)&&(iloop<(elem_num-1))) // Lookup Insert Node
{
p = p->next;
iloop++;
}
if(iloop != (elem_num-1))
{
printf("ERROR:: DLListInsert() (elem_num > (list length + 1)!/n");
return -1;
}
s = (DLinkList*)malloc(sizeof(DLinkList));
if(s == NULL)
{
printf("ERROR:: DLListInsert()->malloc() (not enough memory)!/n");
return -1;
}
if(iloop == 1)
{
p = dq;
}
memcpy(&(s->data), &x, sizeof(ElemType));
s->next = p->next;
if(s->next != NULL)
{
s->next->prev = s;
}
s->prev = p;
p->next = s;
return 0;
}
/*******************************************************
* Get Double Link List Length
*******************************************************/
int DLListGetLength(DLinkList *dq)
{
int iloop = 0;
DLinkList *p;
if(dq == NULL)
{
printf("ERROR:: DLListGetLength() pointer(pararm 1) can't be NULL!/n");
return -1;
}
p = dq;
while(p->next!= NULL)
{
p = p->next;
iloop++;
}
return iloop;
}
/*********************************************************
* Delete Double List
*********************************************************/
int DLListDelete(DLinkList *dq, int elem_num)
{
DLinkList *p;
int iloop = 0;
if(dq == NULL)
{
printf("ERROR:: DLListDelete() pointer(pararm 1) can't be NULL!/n");
return -1;
}
p = dq;
if((DLListGetLength(p) == 0))
{
printf("DLListDelete()-> Element(Head) Can Be Deleted!/n");
return -1;
}
while((p!= NULL)&&(iloop<elem_num)) // Lookup Delete Node
{
p = p->next;
iloop++;
}
if(iloop != elem_num)
{
printf("ERROR:: DLListDelete() (elem_num > list length)!/n");
return -1;
}
p->prev->next = p->next;
if(p->next != NULL)
{
p->next->prev = p->prev;
}
free(p);
return 0;
}
/**********************************************************
* Lookup Double List
**********************************************************/
int DLListLookup(DLinkList *dq, int elem_num, ElemType *x)
{
DLinkList *p;
int iloop = 0;
if(dq == NULL)
{
printf("ERROR:: DLListLookup() pointer(pararm 1) can't be NULL!/n");
return -1;
}
if(x == NULL)
{
printf("ERROR:: DLListLookup() pointer(pararm 3) can't be NULL!/n");
return -1;
}
p = dq;
while((p->next!= NULL)&&(iloop<(elem_num-1))) // Lookup Data Node
{
p = p->next;
iloop++;
}
if(iloop != (elem_num-1))
{
printf("ERROR:: DLListLookup() (elem_num > list length)!/n");
return -1;
}
*x = p->data;
return 0;
}
/*********************************************************
* Destroy Double Link List
*********************************************************/
DLinkList* DLListDestroy(DLinkList *dp)
{
if(dp == NULL)
{
printf("ERROR:: DLListDestroy() pointer(pararm 1) can't be NULL!/n");
}
for(;;)
{
if(DLListDelete(dp, 1) != 0)
{
break;
}
}
free(dp);
return NULL;
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fqheda/archive/2010/09/03/5861778.aspx