2024-12-06 09:38:25 +00:00
|
|
|
|
/*
|
|
|
|
|
* ring_queue.c
|
|
|
|
|
*
|
2024-12-07 09:52:46 +00:00
|
|
|
|
* Created on: 2024年6月21日
|
2024-12-06 09:38:25 +00:00
|
|
|
|
* Author: psx
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2024-12-07 09:52:46 +00:00
|
|
|
|
//循环队列
|
2024-12-06 09:38:25 +00:00
|
|
|
|
#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
|
|
|
|
|
|
2024-12-07 09:52:46 +00:00
|
|
|
|
//初始化队列
|
2024-12-06 09:38:25 +00:00
|
|
|
|
int InitRingQueue(RingQueue *q, RQ_ElementType *buff, int size)
|
|
|
|
|
{
|
|
|
|
|
q->elems = buff;
|
|
|
|
|
q->size = size;
|
|
|
|
|
q->front=q->rear=0;
|
|
|
|
|
return RQ_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-07 09:52:46 +00:00
|
|
|
|
//遍历队列,
|
|
|
|
|
//消费者使用,故对生产者可能修改的rear先读取缓存
|
2024-12-06 09:38:25 +00:00
|
|
|
|
int ShowRingQueue(RingQueue *q)
|
|
|
|
|
{
|
|
|
|
|
//int i;
|
|
|
|
|
int rear = q->rear;
|
|
|
|
|
|
|
|
|
|
if(q->front == rear)
|
|
|
|
|
{
|
2024-12-07 09:52:46 +00:00
|
|
|
|
//rq_debug("队列为空\n");
|
2024-12-06 09:38:25 +00:00
|
|
|
|
return RQ_ERROR;
|
|
|
|
|
}
|
2024-12-07 09:52:46 +00:00
|
|
|
|
//rq_debug("队列中的元素为:\n");
|
2024-12-06 09:38:25 +00:00
|
|
|
|
//for(i=((q->front)%q->size); i != rear; i=((i+1)%q->size))
|
|
|
|
|
//rq_debug(" %c\n",q->elems[i]);
|
|
|
|
|
|
|
|
|
|
//rq_debug("\n");
|
2024-12-07 09:52:46 +00:00
|
|
|
|
//rq_debug("队首元素为%c\n",q->elems[q->front]);
|
|
|
|
|
//rq_debug("队尾元素为%c\n",q->elems[rear - 1]);
|
2024-12-06 09:38:25 +00:00
|
|
|
|
return RQ_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-07 09:52:46 +00:00
|
|
|
|
//向队尾插入元素e
|
2024-12-06 09:38:25 +00:00
|
|
|
|
int InRingQueue(RingQueue *q,RQ_ElementType e)
|
|
|
|
|
{
|
|
|
|
|
if(RingQueueFull(q))
|
|
|
|
|
{
|
2024-12-07 09:52:46 +00:00
|
|
|
|
//rq_debug("空间不足\n");
|
2024-12-06 09:38:25 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-07 09:52:46 +00:00
|
|
|
|
//从队首取回并删除元素
|
2024-12-06 09:38:25 +00:00
|
|
|
|
int OutRingQueue(RingQueue *q, RQ_ElementType *e)
|
|
|
|
|
{
|
|
|
|
|
if(RingQueueEmpty(q))
|
|
|
|
|
{
|
2024-12-07 09:52:46 +00:00
|
|
|
|
//rq_debug("队列为空\n");
|
2024-12-06 09:38:25 +00:00
|
|
|
|
return RQ_ERROR;
|
|
|
|
|
}
|
|
|
|
|
*e = q->elems[q->front];
|
2024-12-07 09:52:46 +00:00
|
|
|
|
//rq_debug("被删除的队首元素为%c\n",q->elems[q->front]);
|
2024-12-06 09:38:25 +00:00
|
|
|
|
q->front = (q->front+1) % q->size;
|
|
|
|
|
return RQ_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-07 09:52:46 +00:00
|
|
|
|
//队列中的元素个数
|
2024-12-06 09:38:25 +00:00
|
|
|
|
int RingQueueLength(RingQueue *q)
|
|
|
|
|
{
|
|
|
|
|
return ((q->rear - q->front) + q->size) % q->size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|