/* 队列的链式存储 */ /* with header */ /* with no typedef */ struct Node{ ElementType Ele; struct Node *Next; }; struct LinQ{ struct Node *rear; struct Node *front; }; struct LinQ * CreateQ( void ) { struct LinQ *Ptr; struct Node *header; Ptr = malloc(sizeof(struct LinQ)); if(Ptr == NULL ) Error("out of space "); header = malloc( sizeof( struct Node ) ); if(header == NULL ) error("out of space "); header->Next = NULL; Ptr->front = Ptr->rear = header; } void AddQ( struct LinQ *PtrQ, ElementType X ) { struct Node *Ptr; Ptr = malloc(sizeof(struct Node )); if(Ptr == NULL) Error("out of space "); Ptr->Ele = X; Ptr->Next = NULL; PtrQ->rear->Next = Ptr; PtrQ->rear = Ptr; } int IsEmpty( struct LinQ * PtrQ ) { return PtrQ->front->Next == NULL; } ElementType DeleteQ( struct LinQ * PtrQ ) { struct Node * TmpCell; if( IsEmpty( PtrQ ) ) Error("empty Queue"); Tmpcell = PtrQ->front->Next; PtrQ->front->Next = TmpCell->Next; free( TmpCell ); }
CreateQ旨在创造一个front和rear都指向头结点header的LinQ结构
头结点处Next设为NULL,在判断是否空队时可用
front一直指到头结点
新增结点的Next一定改为NULL
结论:可以用typedef的地方就用,可以用头结点的地方就用
时间: 2024-10-10 01:31:43