0%

Circular Queue

循环队列

结构代码

1
2
3
4
5
6
7
8
9
10

typedef int QElemType;

typedef struct
{
QElemType data[MAXSIZE];
int *front;
int *rear;
} SqQueue;

初始化代码

1
2
3
4
5
6
7
8
9

Status InitQueue(SqQueue *Q)
{
Q->front = NULL;
Q->rear = NULL:

return OK;
}

循环队列求队列长度

1
2
3
4
5
6

int QueueLength(SqQueue Q)
{
return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}

循环队列入队列

1
2
3
4
5
6
7
8
9
10
11

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;
}

循环队列出队列

1
2
3
4
5
6
7
8
9
10
11

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;
}

Welcome to my other publishing channels