1. 结构体定义
typedef struct {
int data[MAXSIZE];
int qFront, qRear;
}CycleQueue;
2. 初始化
void CycleQueueInit(CycleQueue *cq)
{
cq->qFront = 0;
cq->qRear = 0;
}
3. 队列判空
int CycleQueueEmpty(CycleQueue *cq)
{
return cq->qFront == cq->qRear ? 1 : -1;
}
4. 队列判满
int CycleQueueFull(CycleQueue *cq)
{
return (cq->qRear + 1) % MAXSIZE == cq->qFront ? 1 : -1;
}
5. 入队
int CycleQueueEnQueue(CycleQueue *cq, int e)
{
if(CycleQueueFull(cq) > 0)
return -1;
cq->data[cq->qRear] = e;
cq->qRear = (cq->qRear + 1) % MAXSIZE;
return 1;
}
6. 出队
int CycleQueueDeQueue(CycleQueue *cq, int *e)
{
if(CycleQueueEmpty(cq) > 0)
return -1;
*e = cq->data[cq->qFront];
cq->qFront = (cq->qFront + 1) % MAXSIZE;
return 1;
}
|