/* * An example for queue implemented in fixed size array * usually called circular or ring buffer */ #include #include #define SIZE 100 typedef struct { int data[SIZE]; int head; int tail; } Queue; void enqueue(Queue* q, int d) { q->data[q->tail] = d; if(q->tail==SIZE-1) q->tail = 0; else q->tail = q->tail+1; } int dequeue(Queue* q) { int temp = q->data[q->head]; if(q->head==SIZE-1) q->head = 0; else q->head = q->head+1; return temp; } void init(Queue* q) { Q->head = 0; q->tail = 0; } void print_queue(Queue* q) { if(q->tail==q->head) { printf("empty queue.\n"); return; } if(q->tail>q->head) { int len = q->tail-q->head; while(len) { printf("%d ", q->data[q->tail-len]); len --; } printf("\n"); } // what if q->tail < q->head? add your code below } int main() { Queue q; init(&q); print_queue(&q); enqueue(&q, 1); print_queue(&q); enqueue(&q, 2); print_queue(&q); return 0; }