c语言广义链表ADT,链表ADT C语言实现

/*place in the implementation file*/

struct Node

{

ElementType Element;

Position Next;

};

/*return true if L is empty,判断链表是否为空*/

int IsEmpty(List L)

{

return P->Next==NULL;

}

/*return true if P is the last position in list L*/

/*Parameter L is unused in this implementation*/

/*测试当前位置是否是链表末尾*/

int IsLast(Position P,List L)

{

return P->Next==NULL;

}

/*return Position of X in L;NULL if not found*/

/*find例程*/

Position Find(ElementType X,List L)

{

Position P;

P=L->Next;

while(P!=NULL&&P->Element!=X)

P=P->Next;

return P;

}

/*Delete first occurrence of X from a List*/

/*Assume use of a header node */

/*链表的删除例程*/

void Delete(ElementType X,List L)

{

Position P,TmpCell;

P=FindPrevious(X,L);

if(!IsLast(P,L))

{

TmpCell=P->Next;

p->Next=TmpCell->Next;

free(TmpCell);

}

}

/*if x is not found , then next field of returned*/

/*position is NULL*/

/*Assume a header*/

/*Findprevious例程*/

Position FindPrevious(ElementType X,List L)

{

Position P;

P=L;

while(P->Next!=NULL&&P->Next->Element!=X)

{

P=P->Next;

}

return P;

}

/*insert(after legal position)*/

/*Header implemtentation assumed*/

/*parameter L is unused in this implementation*/

/*链表的插入例程*/

void Insert(ElementType X,List L)

{

Position TmpCell;

TmpCell=malloc(sizeof(struct Node));

if(TmpCell==NULL)

{

fprintf(stderr,"out of space");

error(1);

}

TmpCell->Element=X;

TmpCell->Next=P->Next;

p->Next=TmpCell;

}

/*删除表*/

void DeleteList(List L)

{

Position P,Tmp;

P=L->Next;/*header assumed*/

L->Next=NULL;

while(P!=NULL)

{

Tmp=P->Next;

free(P);

P=Tmp;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值