1. 程式人生 > >循環雙鏈表

循環雙鏈表

listnode osi test poi sem malloc while tty type

#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;

struct DoubleCircularListNode
{
    ElementType Element;
    struct DoubleCircularListNode *Prior;
    struct DoubleCircularListNode *Next;
};

struct DoubleCircularListNode* DoubleCircularListInit()
{
    struct DoubleCircularListNode* ListHead = malloc
(sizeof(struct DoubleCircularListNode)); ListHead -> Prior = ListHead -> Next = ListHead; return ListHead; } int DoubleCircularListNodeAdd(struct DoubleCircularListNode *ListHead,ElementType ToBeInsert) { struct DoubleCircularListNode* TmpCell; struct DoubleCircularListNode* LastNode; TmpCell
= malloc(sizeof(struct DoubleCircularListNode)); TmpCell -> Element = ToBeInsert; LastNode = ListHead -> Prior; TmpCell -> Prior = LastNode; TmpCell -> Next = ListHead; ListHead -> Prior = LastNode -> Next = TmpCell; return 0; } int DoubleCircularListIsEmpty(struct
DoubleCircularListNode *ListHead) { return (ListHead->Next == ListHead); } int DoubleCircularListIsLast(struct DoubleCircularListNode *PtrToNode,struct DoubleCircularListNode *ListHead) { return (PtrToNode -> Next == ListHead); } //Warning:ensure the return pointer doesn‘t point to ListHead struct DoubleCircularListNode* DoubleCircularListNodeFind(ElementType ToBeFind,struct DoubleCircularListNode *ListHead) { struct DoubleCircularListNode* Pointer; Pointer = ListHead -> Next; while(Pointer != ListHead && Pointer -> Element != ToBeFind) { Pointer = Pointer -> Next; } return Pointer; } int DoubleCircularListNodeDelete(ElementType ToBeDelete,struct DoubleCircularListNode *ListHead) { struct DoubleCircularListNode *Pointer; Pointer = DoubleCircularListNodeFind(ToBeDelete,ListHead); if(Pointer == ListHead) { return 1; } else if(Pointer -> Prior == ListHead) { ListHead -> Prior = ListHead -> Next = ListHead; free(Pointer); return 0; } else { Pointer -> Next -> Prior = Pointer -> Prior; Pointer -> Prior -> Next = Pointer -> Next; free(Pointer); return 0; } return 1; } int MakeDoubleCircularListEmpty(struct DoubleCircularListNode *ListHead) { struct DoubleCircularListNode *Pointer_1; struct DoubleCircularListNode *Pointer_2; if(ListHead != NULL) { Pointer_1 = ListHead; Pointer_2 = ListHead -> Next; while(Pointer_2 != ListHead) { Pointer_1 = Pointer_2; Pointer_2 = Pointer_2 -> Next; free(Pointer_1); } ListHead -> Prior = ListHead -> Next = ListHead; } else { return 1; } return 0; } int DoubleCircularListDelete(struct DoubleCircularListNode *ListHead) { MakeDoubleCircularListEmpty(ListHead); free(ListHead); return 0; } int DoubleCircularListNodeInsert(struct DoubleCircularListNode *ListHead,ElementType ToBeInsert,struct DoubleCircularListNode *PositionBeforeToBeInsert) { struct DoubleCircularListNode* TmpCell; TmpCell = malloc(sizeof(struct DoubleCircularListNode)); TmpCell -> Element = ToBeInsert; if(TmpCell == NULL) { return 1; } TmpCell -> Prior = PositionBeforeToBeInsert; TmpCell -> Next = PositionBeforeToBeInsert -> Next; PositionBeforeToBeInsert -> Next -> Prior = TmpCell; PositionBeforeToBeInsert -> Next = TmpCell; return 0; } int main() { struct DoubleCircularListNode* ListHead; ListHead = DoubleCircularListInit(); DoubleCircularListNodeInsert(ListHead,1,ListHead); DoubleCircularListNodeInsert(ListHead,3,DoubleCircularListNodeFind(1,ListHead)); DoubleCircularListNodeAdd(ListHead,4); DoubleCircularListNodeInsert(ListHead,2,DoubleCircularListNodeFind(1,ListHead)); int test = DoubleCircularListNodeDelete(2,ListHead); printf("test = %d %d\n",test,ListHead -> Next -> Element); printf("%d\n",ListHead -> Next -> Next -> Element); //printf("%d\n",ListHead -> Next -> Next -> Next -> Element); test = DoubleCircularListNodeDelete(2,ListHead); printf("test = %d %d\n",test,ListHead -> Next -> Next -> Element); //printf("%d\n",ListHead -> Next -> Next -> Next -> Element); //printf("%d\n",DoubleCircularListNodeFind(3,ListHead)->Element); DoubleCircularListDelete(ListHead); return 0; }

循環雙鏈表