mppt/Drivers/RingQueue2/ring_queue2.c

92 lines
1.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* ring_queue.c
*
* Created on: 2024年6月21日
* Author: psx
*/
//循环队列
#include <stdio.h>
#include <stdlib.h>
#include "ring_queue2.h"
//#define RING_QUEUE_DEBUG
//#ifdef RING_QUEUE_DEBUG
//#define //rq_debug term_printf
//#else
//void rq_debug(const char *fmt, ...){};
//#endif
#define RQ_OK 1
#define RQ_ERROR 0
#define RQ_OVERFLOW -2
//初始化队列
int InitRingQueue2(RingQueue2 *q, RQ_ElementType2 *buff, int size)
{
q->elems = buff;
q->size = size;
q->front=q->rear=0;
return RQ_OK;
}
//遍历队列,
//消费者使用故对生产者可能修改的rear先读取缓存
int ShowRingQueue2(RingQueue2 *q)
{
//int i;
int rear = q->rear;
if(q->front == rear)
{
//rq_debug("队列为空\n");
return RQ_ERROR;
}
//rq_debug("队列中的元素为:\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("队首元素为%c\n",q->elems[q->front]);
//rq_debug("队尾元素为%c\n",q->elems[rear - 1]);
return RQ_OK;
}
//向队尾插入元素e
int InRingQueue2(RingQueue2 *q,RQ_ElementType2 e)
{
if(RingQueueFull2(q))
{
//rq_debug("空间不足\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;
}
//从队首取回并删除元素
int OutRingQueue2(RingQueue2 *q, RQ_ElementType2 *e)
{
if(RingQueueEmpty2(q))
{
//rq_debug("队列为空\n");
return RQ_ERROR;
}
*e = q->elems[q->front];
//rq_debug("被删除的队首元素为%c\n",q->elems[q->front]);
q->front = (q->front+1) % q->size;
return RQ_OK;
}
//队列中的元素个数
int RingQueueLength2(RingQueue2 *q)
{
return ((q->rear - q->front) + q->size) % q->size;
}