Circular Queue
31 May 2020
1 min read
循环队列
结构代码
typedef int QElemType;
typedef struct
{
QElemType data[MAXSIZE];
int *front;
int *rear;
} SqQueue;
初始化代码
Status InitQueue(SqQueue *Q)
{
Q->front = NULL;
Q->rear = NULL:
return OK;
}
循环队列求队列长度
int QueueLength(SqQueue Q)
{
return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}
循环队列入队列
Status EnQueue(SqQueue *Q, QElemType e)
{
if ((Q->rear + 1) % MAXSIZE == Q->front)
return ERROR;
Q->data[Q->rear] = e;
Q->rear = (Q->rear + 1) % MAXSIZE;
return OK;
}
循环队列出队列
Status DeQueue(SqQueue *Q, QElemType *e)
{
if (Q->front == Q->rear)
return ERROR;
*e = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;
return OK;
}