/** * print all items in queue * @param Q the pointer of queue */ voidprintQueue(Queue* Q) { // 要知道队列当前有多少个元素 int length = (Q->rear - Q->front + MAXSIZE) % MAXSIZE; int index = Q->front; for (int i = 0; i < length; i++) { printf("%d -> ", Q->data[index]); index = (index + 1) % MAXSIZE; } printf("NULL\n"); }
/** * judge queue is or not full * @param Q the pointer of queue * @return full flag */ intisFull(Queue* Q) { if ((Q->rear + 1) % MAXSIZE == Q->front) { return1; } else { return0; } }
/** * judge queue is or not empty * @param Q the pointer of queue * @return empty flag */ intisEmpty(Queue* Q) { if (Q->front == Q->rear) { return1; } else { return0; } }
/** * enqueue * @param Q the pointer of queue * @param data the data you want to enqueue * @return success flag */ intenQueue(Queue* Q, int data) { if (isFull(Q)) { return0; } else { Q->data[Q->rear] = data; Q->rear = (Q->rear + 1) % MAXSIZE; return1; } }
/** * dequeue * @param Q the pointer of queue * @return the data you want to dequeue */ intdeQueue(Queue* Q) { if (isEmpty(Q)) { return-1; } else { int data = Q->data[Q->front]; Q->front = (Q->front + 1) % MAXSIZE; return data; } }