#include <stdio.h>#include<stdlib.h>#defineMax_size 10/*to implement a queue with a dynamic array*/typedefstructQueue {intcapacity; intFront; intRear; intSize; intdata[max_size];} Queue;voidError (Char*error) {printf ("%s", error);}voidFatalError (Char*fatalerror) {printf ("%s", fatalerror);}intIsEmpty (Queue *Q) {returnQ->size = =0;}intIsfull (Queue *Q) {returnQ->size = = q->capacity;}voidInit (Queue *q) {q->size =0; Q->front =1; Q->rear =0; Q->capacity =max_size;}Static intSUCC (intValue,queue *Q) {if(++value = = q->capacity) {Value=0; } returnvalue;}voidEnqueue (intX,queue *Q) {if(Isfull (Q)) FatalError ("Full queue"); Else{Q->size++; Q->rear = SUCC (q->rear,q); Q->data[Q->rear] =X; }} voidDequeue (Queue *Q) {if(IsEmpty (Q)) FatalError ("Empty Queue"); Else{Q->size--; Q->front = SUCC (q->front,q); }}intFrontanddequeue (Queue *Q) {intTmp; if(IsEmpty (Q)) Error ("Empty Queue"); Else{Q->size--; TMP= q->data[q->Front]; Q->front = SUCC (q->front,q); returnTmp; }}voidDisposequeue (Queue *Q) { Free(q->data); Free(Q);} Main () {Queue*Q; Init (q); inti; for(i =0; I <MAX_SIZE; i++) {Enqueue (i,q); } for(i =0; I <MAX_SIZE; i++) {printf ("%d\n", Frontanddequeue (q)); }}
C language implementation of common data structures--queue