//linked list to do queue/*assume a header*/structNode;structQueue;typedefstructNode *Ptrtonode;typedefstructQueue *Ptrtoq;structnode{ElementType ele; Ptrtonode Next;};structqueue{Ptrtonode Rear; Ptrtonode Front;}; Ptrtoqcreateq (void) {Ptrtoq Q; Q=malloc(sizeof(structQueue)); Q->rear = Q->front =malloc(sizeof(structNode)); Q->rear->next =NULL;}voidADDQ (ElementType X, Ptrtoq Q) {Ptrtonode Tmpcell; Tmpcell=malloc(sizeof(structNode)); Tmpcell->ele =X; Tmpcell->next =NULL; Q->rear->next =Tmpcell; Q->rear =Tmpcell;} Elementtypedeleteq (Ptrtoq Q) {ElementType res; Ptrtonode Tmpcell; Tmpcell= q->front->Next; Q->front->next = tmpcell->Next; Res= tmpcell->Ele; Free(Tmpcell); returnRes;}
View Code
The above is done using the linked list
The following is an array of
//Loop Array#defineMaxSize 100structQueue;typedefstructQueue *Ptrtoq;structqueue{ElementType*Array; intFront; intrear;}; Ptrtoqcreateq (intSize) {Ptrtoq Q; Q=malloc(sizeof(structQueue)); Q->array =malloc(sizeof(ElementType) *Size); Q->front = Q->rear =0; returnQ;}intisfull (Ptrtoq Q) {return(Q->rear +1)% MaxSize = = q->Front;}voidADDQ (ElementType X, Ptrtoq Q) {if( !isfull (Q)) {Q->rear = (Q->rear +1) %MaxSize; Q->array[Q->rear] =X; } ElseError (" Full")}intIsEmpty (Ptrtoq Q) {returnQ->rear = = q->Front;} Elementtypedeleteq (Ptrtoq Q) {if( !IsEmpty (Q)) {Q->front = (Q->front +1) %MaxSize; returnQ->front->Ele; } ElseError ();}
View Code
In this column of the loop array, actually the place that front points to is actually meaningless but to keep, is empty
Exercise 3.25 List and array-to-queue routines implementation