1.1单链表

    技术2022-05-11  125

    1.1单链表-9.28

    #ifndef _List_H

    struct Nodetypedef struct Node *PtrToNode;typedef PtrToNode Position;typedef PtrToNode List;

    List MakeEmpty(List L);int IsEmpty(List L);int IsLast(Position P,List L);Position Find(ElementType X,List L);void Delete(ElementType X,List L);Position FindPrevious(ElementType X,List L);void Insert(ElementType X,Position P,List L);void DeletList(List L);

    #endif

    struct Node{ ElementType Element; Position Next;}; /****************************************/int IsEmpty(List L){return L->Next==NULL;}/*******************************************/int IsLast(Position P,List L){return P->Next==NULL;}/******************************************/Position Find(ElementType X,List L){Position P;P=L->Next;while(X!=P->Element&&P!=NULL)   /*不要忘记p!=NULL这一个条件!*/ P=P->Next;return P;}/****************************************/void Delete(Element X,List L){Position P,TmpCell;P=FindPrevious(X,L);if(P!=IsLast(P,L)) { TmpCell=P; P->Next=TmpCell->Next; free(TmpCell); }}/****************************************/Position FindPrevious(ElmentType X,List L){Position P;P=L;if(P->Next->Elment!=X&&P->Next!=NULL) P=P->Next;return P;}/*****************************************/void Insert(ElementType X,Position P,List L){Position TmpCell;TmpCell=malloc(sizeof(struct node));if(TmpCell==NULL) FatalError("Out of space!!");/*这个检查机制是必不可少的*/TmpCell->Elment=X;TmpCell->Next=P->Next;P->Next=TmpCell;}

    /******************************************/void DeleteList(List L){Position P,Tmp;P=L->Next;                  /*如果漏掉此行,那么P就是未定义的,不可能指向内存有效部,非法*/L->Next=NULL;while(P->Next!=NULL) { Tmp=P->NEXT;         /*如果我们没有在函数中用这一步,而是直接free(P);P=P->NEXT; free(P);             /*则会出现错误,这是一种常见的错误 P=Tmp; }

    }


    最新回复(0)