标签:empty single ext amp null class color make 单链表
#include <stdio.h> #include <stdlib.h> typedef int ElementType; struct SingleListNode { ElementType Element; struct SingleListNode *Next; }; struct SingleListNode* SingleListInit() { struct SingleListNode* ListHead = malloc(sizeof(struct SingleListNode)); ListHead -> Next = NULL; return ListHead; } int MakeSingleListEmpty(struct SingleListNode *ListHead) { struct SingleListNode *Pointer_1; struct SingleListNode *Pointer_2; if(ListHead != NULL) { Pointer_1 = ListHead; Pointer_2 = ListHead -> Next; while(Pointer_2 != NULL) { Pointer_1 = Pointer_2; Pointer_2 = Pointer_2 -> Next; free(Pointer_1); } ListHead -> Next = NULL; } else { return 1; } return 0; } int SingleListIsEmpty(struct SingleListNode *ListHead) { return (ListHead->Next == NULL); } int SingleListNodeIsLast(struct SingleListNode *PtrToNode,struct SingleListNode *ListHead) { return (PtrToNode -> Next == NULL); } struct SingleListNode* SingleListNodeFind(ElementType ToBeFind,struct SingleListNode *ListHead) { struct SingleListNode* Pointer; Pointer = ListHead -> Next; while(Pointer != NULL && Pointer -> Element != ToBeFind) { Pointer = Pointer -> Next; } return Pointer; } struct SingleListNode* SingleListNodeFindPrevious(ElementType ToBeFind,struct SingleListNode *ListHead) { struct SingleListNode* Pointer; Pointer = ListHead; while(Pointer -> Next != NULL && Pointer -> Next -> Element != ToBeFind) { Pointer = Pointer -> Next; } return Pointer; } int SingleListNodeDelete(ElementType ToBeDelete,struct SingleListNode *ListHead) { struct SingleListNode* Pointer; struct SingleListNode* TmpCell; Pointer = SingleListNodeFindPrevious(ToBeDelete,ListHead); if(!SingleListNodeIsLast(Pointer,ListHead)) { TmpCell = Pointer -> Next; Pointer -> Next = TmpCell -> Next; free(TmpCell); return 0; } return 1; } int SingleListNodeInsert(ElementType ToBeInsert,struct SingleListNode *ListHead,struct SingleListNode *PositionBeforeToBeInsert) { struct SingleListNode *TmpCell; TmpCell = malloc(sizeof(struct SingleListNode)); if(TmpCell == NULL) { return 1; } TmpCell -> Element = ToBeInsert; TmpCell -> Next = PositionBeforeToBeInsert -> Next; PositionBeforeToBeInsert -> Next = TmpCell; return 0; } int SingleListNodeAdd(ElementType ToBeInsert,struct SingleListNode *ListHead) { struct SingleListNode *TmpCell; struct SingleListNode *LastNode; TmpCell = malloc(sizeof(struct SingleListNode)); if(TmpCell == NULL) { return 1; } LastNode = ListHead; while(!SingleListNodeIsLast(LastNode,ListHead)) { LastNode = LastNode -> Next; } TmpCell -> Element = ToBeInsert; TmpCell -> Next = LastNode -> Next; LastNode -> Next = TmpCell; return 0; } int SingleListDelete(struct SingleListNode *ListHead) { struct SingleListNode *Pointer; if(ListHead == NULL) { return 1; } while(ListHead != NULL) { Pointer = ListHead; ListHead = ListHead -> Next; free(Pointer); } return 0; } int main() { struct SingleListNode *ListHead = SingleListInit(); SingleListNodeAdd(1,ListHead); SingleListNodeAdd(2,ListHead); SingleListNodeInsert(3,ListHead,SingleListNodeFindPrevious(2,ListHead)); SingleListNodeDelete(1,ListHead); SingleListNodeDelete(3,ListHead); printf("%d\n",ListHead->Next->Element); MakeSingleListEmpty(ListHead); //printf("%d\n",ListHead->Next->Element); SingleListDelete(ListHead); return 0; }
标签:empty single ext amp null class color make 单链表
原文地址:https://www.cnblogs.com/Asurudo/p/9427377.html