/* 队列的链式存储 */ /* with no header */ struct Node; struct LinkQueue; typedef struct Node *PtrToNode; typedef struct LinkQueue *PtrTorf; struct Node{ ElementType X; PtrToNode Next; }; struct LinkQueue{ PtrToNode rear; PtrToNode front; }; int IsEmpty(PtrTorf PtrQ ) { return PtrQ->front == NULL; } ElemenType DeleQ(PtrTorf PtrQ ) { PtrToNode TmpCell; ElementType X; if( IsEmpty( PtrQ ) ) { Error("队列是空"); retutn; } TmpCell = PtrQ->front; X = TmpCell->Element; if( PtrQ->front == PtrQ->rear ) { PtrQ->front = NULL; PtrQ->rear = NULL; } else PtrQ->front = TmpCell->Next; free( TmpCell ); return X; } void AddQ( PtrTorf PtrQ ) { PtrToNode TmpCell; TmpCell = malloc(sizeof(struct Node )); if(TmoCell == NULL ) Error("out if space "); TmpCell->Next == NULL; TmpCell->Element = X; if( !(IsEmpy( PtrQ ))){//队列非空时,也只有rear再动 PtrQ->rear->Next = TmpCell; PtrQ->rear = TmpCell; } else PtrQ->rear = Ptr->front = TmpCell;//队列的链式存储,front和rear都指向一处时,才表示有一个元素,如果都指向NULL就表示空 }
时间: 2024-11-05 05:22:31