88 lines
1.7 KiB
C
88 lines
1.7 KiB
C
|
/*
|
|||
|
* ring_queue.c
|
|||
|
*
|
|||
|
* Created on: 2024<EFBFBD><EFBFBD>6<EFBFBD><EFBFBD>21<EFBFBD><EFBFBD>
|
|||
|
* Author: psx
|
|||
|
*/
|
|||
|
|
|||
|
|
|||
|
//ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
#include "ring_queue.h"
|
|||
|
|
|||
|
//#define RING_QUEUE_DEBUG
|
|||
|
|
|||
|
//#ifdef RING_QUEUE_DEBUG
|
|||
|
//#define //rq_debug term_printf
|
|||
|
//#else
|
|||
|
//void rq_debug(const char *fmt, ...){};
|
|||
|
//#endif
|
|||
|
|
|||
|
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
int InitRingQueue(RingQueue *q, RQ_ElementType *buff, int size)
|
|||
|
{
|
|||
|
q->elems = buff;
|
|||
|
q->size = size;
|
|||
|
q->front=q->rear=0;
|
|||
|
return RQ_OK;
|
|||
|
}
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ã<EFBFBD><C3A3>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߿<EFBFBD><DFBF><EFBFBD><EFBFBD>ĵ<DEB8>rear<61>ȶ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
|||
|
int ShowRingQueue(RingQueue *q)
|
|||
|
{
|
|||
|
//int i;
|
|||
|
int rear = q->rear;
|
|||
|
|
|||
|
if(q->front == rear)
|
|||
|
{
|
|||
|
//rq_debug("<22><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>\n");
|
|||
|
return RQ_ERROR;
|
|||
|
}
|
|||
|
//rq_debug("<22><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Ԫ<EFBFBD><D4AA>Ϊ:\n");
|
|||
|
//for(i=((q->front)%q->size); i != rear; i=((i+1)%q->size))
|
|||
|
//rq_debug(" %c\n",q->elems[i]);
|
|||
|
|
|||
|
//rq_debug("\n");
|
|||
|
//rq_debug("<22><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>Ϊ%c\n",q->elems[q->front]);
|
|||
|
//rq_debug("<22><>βԪ<CEB2><D4AA>Ϊ%c\n",q->elems[rear - 1]);
|
|||
|
return RQ_OK;
|
|||
|
}
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>e
|
|||
|
int InRingQueue(RingQueue *q,RQ_ElementType e)
|
|||
|
{
|
|||
|
if(RingQueueFull(q))
|
|||
|
{
|
|||
|
//rq_debug("<22>ռ䲻<D5BC><E4B2BB>\n");
|
|||
|
return(RQ_OVERFLOW);
|
|||
|
}
|
|||
|
q->elems[q->rear] = e;
|
|||
|
q->rear = (q->rear+1) % q->size;
|
|||
|
//rq_debug("rear = %d\n",q->rear);
|
|||
|
return RQ_OK;
|
|||
|
}
|
|||
|
|
|||
|
//<2F>Ӷ<EFBFBD><D3B6><EFBFBD>ȡ<EFBFBD>ز<EFBFBD>ɾ<EFBFBD><C9BE>Ԫ<EFBFBD><D4AA>
|
|||
|
int OutRingQueue(RingQueue *q, RQ_ElementType *e)
|
|||
|
{
|
|||
|
if(RingQueueEmpty(q))
|
|||
|
{
|
|||
|
//rq_debug("<22><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>\n");
|
|||
|
return RQ_ERROR;
|
|||
|
}
|
|||
|
*e = q->elems[q->front];
|
|||
|
//rq_debug("<22><>ɾ<EFBFBD><C9BE><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>Ԫ<EFBFBD><D4AA>Ϊ%c\n",q->elems[q->front]);
|
|||
|
q->front = (q->front+1) % q->size;
|
|||
|
return RQ_OK;
|
|||
|
}
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>
|
|||
|
int RingQueueLength(RingQueue *q)
|
|||
|
{
|
|||
|
return ((q->rear - q->front) + q->size) % q->size;
|
|||
|
}
|
|||
|
|
|||
|
|