这段代码使用数组实现了一个循环队列。重点在于判满与判空。
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#define MAXQUEUE 20
//Message Queue to manage the PCL_MSG
typedef struct tagMsgQueue
{
int uFront,uRear;
int m_Msg[MAXQUEUE];
}MSGQUEUE;
#define Q_FULL(m) ((((m)->uRear + 1) % MAXQUEUE) == ((m)->uFront % MAXQUEUE))
#define Q_EMPTY(m) (((m)->uRear % MAXQUEUE) == ((m)->uFront % MAXQUEUE))
#define Q_INIT(m) \
(m)->uFront = 0;\
(m)->uRear = 0;\
bool EnQueue(MSGQUEUE *pMsg, int msg)
{
int nIntEn;
//TODO:Need add enter critical sections codes
if (!Q_FULL(pMsg))
{
nIntEn = (pMsg->uRear++) % MAXQUEUE;
pMsg->m_Msg[nIntEn] = msg;
return true;
}
return false;
}
bool DeQueue(MSGQUEUE *pMsg, int *pDeMsg)
{
if (!Q_EMPTY(pMsg))
{
*pDeMsg = pMsg->m_Msg[pMsg->