C語言隊列

C對列隊列,先進先出。大神看下有毛病沒。

注意:定義MAXQUEUE 20字節,只能存儲19字節的內容。有一字節空,是判斷隊列是否滿的標誌。

#define MAXQUEUE 20
typedef struct tagMsgQueue
{   
    int uFront,uRear;    
    int m_Msg[MAXQUEUE];
}MSGQUEUE; 

#define Q_FULL(m) ((((m)->uRear + 1) % MAXQUEUE) == (m)->uFron)
#define Q_EMPTY(m) ((m)->uRear == (m)->uFront )
#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;        
        pMsg->m_Msg[pMsg->uRear++] = msg;
        pMsg->uRear = pMsg->uRear % MAXQUEUE;
        
        return true;    
    }    
    return false;
} 

bool DeQueue(MSGQUEUE *pMsg, int *pDeMsg)
{    if (!Q_EMPTY(pMsg))    
     {        
        *pDeMsg = pMsg->m_Msg[pMsg->uFront];        
        pMsg->uFront = (pMsg->uFront+1)%MAXQUEUE;        
        return true;    
     }    
     else    
     {        
        Q_INIT(pMsg);        
        return false;    
      }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章