/*循环链表的存储结构*/
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;
typedef struct DNode
{
ElemType data;
struct DNode *prior;
struct DNode *next;
}dnode;
typedef struct DNode *DoubleLink;
DoubleLinkList.h
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;
typedef struct DNode
{
ElemType data;
struct DNode *prior;
struct DNode *next;
}dnode;
typedef struct DNode *DoubleLink;
Status InitLink(DoubleLink *DL);/*双向链表初始化*/
Status CreatLink(DoubleLink *DL, int n);/*创建双向链表*/
Status InsertLink(DoubleLink *DL, int i, ElemType e);/*双向链表的插入*/
Status DeleteLink(DoubleLink *DL, int i, ElemType *e);/*双向链表的删除*/
int LenLink(DoubleLink dl);/*双向链表的长度*/
void PrintList(DoubleLink dl);/*打印双向链表*/
DoubleLinkList.c
Status InitLink(DoubleLink *DL)
{
*DL = (DoubleLink)malloc(sizeof(dnode));
if(!(*DL))
{
return ERROR;
}
(*DL)->next = (*DL);
(*DL)->prior = (*DL);
return OK;
}
Status CreatLink(DoubleLink *DL, int n)
{
int i;
DoubleLink r, p;
InitLink(DL);
r = *DL;
for(i=0; i<n; i++)
{
p = (DoubleLink)malloc(sizeof(dnode));
if(!p)
{
return ERROR;
}
p->data = i+1;
r->next = p;
p->prior = r;
r = p;
}
r->next = NULL;
return OK;
}
Status InsertLink(DoubleLink *DL, int i, ElemType e)
{
int j = 0;
DoubleLink r, p;
r = (*DL);
if(i<1 || i>LenLink(*DL)+1)
{
return ERROR;
}
else if(i<LenLink(*DL)+1)
{
for(j=1; j<i; j++)
{
r = r->next;
}
p = (DoubleLink)malloc(sizeof(dnode));
if(!p)
{
return ERROR;
}
p->data = e;
/*
p->prior = r;
p->next = r->next;
r->next->prior = p;
r->next = p;
*/
p->next = r->next;
r->next->prior = p;
r->next = p;
p->prior = r;
}
else if(i==LenLink(*DL)+1)
{
for(j=1; j<i; j++)
{
r = r->next;
}
p = (DoubleLink)malloc(sizeof(dnode));
if(!p)
{
return ERROR;
}
p->data = e;
r->next = p;
p->prior = r;
p->next = NULL;
}
else;
return OK;
}
Status DeleteLink(DoubleLink *DL, int i, ElemType *e)
{
int j = 0;
DoubleLink r = *DL;
if(i<1 || i>LenLink(*DL))
{
return ERROR;
}
else if(i<LenLink(*DL))
{
for(j=0; j<i; j++)
{
r = r->next;
}
*e = r->data;
r->prior->next = r->next;
r->next->prior = r->prior;
free(r);
}
else if(i==LenLink(*DL))
{
for(j=0; j<i; j++)
{
r = r->next;
}
*e = r->data;
r->prior->next = NULL;
free(r);
}
else;
return OK;
}
int LenLink(DoubleLink dl)
{
int len = 0;
while(dl->next != NULL)
{
dl = dl->next;
len++;
}
return len;
}
void PrintList(DoubleLink dl)
{
while(dl->next != NULL)
{
dl = dl->next;
printf("%d ", dl->data);
}
printf("\n");
}
&spm=1001.2101.3001.5002&articleId=89354676&d=1&t=3&u=04dd23dc58434f2b9fe39a45fccbf058)
871

被折叠的 条评论
为什么被折叠?



