#include <stdio.h>
#include <stdlib.h>
/*重要*/int g_QueueModeUsed=1; //此处选择队列类型:1->数据覆盖 0->数据存满停止,待出队!
#define bool int
#define TRUE 1
#define FALSE 0
#define Report printf
typedef struct queue_data_type_structure
{
int bFlag;
char chBuf[128];
char reserve[4];
}QUEUE_DATA_TYPE_STRUCTURE,*PQUEUE_DATA_TYPE_STRUCTURE;
typedef struct queue
{
QUEUE_DATA_TYPE_STRUCTURE *pBase;
int nFront; //指向队列第一个元素
int nRear; //指向队列最后一个元素的下一个元素
int nMaxSize; //循环队列的最大存储空间
}QUEUE,*PQUEUE;
/*****************************************************************************
Prototype : CreateQueue
Description : 创建队列
Input : PQUEUE Q
int nMaxSize
History :
1.Date : 2016/12/6
Author : Pan
Modification : Created function
*****************************************************************************/
void CreateQueue(PQUEUE Q,int nMaxSize)
{
Q->pBase=(QUEUE_DATA_TYPE_STRUCTURE *)malloc(sizeof(QUEUE_DATA_TYPE_STRUCTURE)*nMaxSize);
if(NULL==Q->pBase)
{
Report("Memory allocation failure");
return ;
}
Q->nFront=0;//初始化参数
Q->nRear=0;
Q->nMaxSize=nMaxSize;
}
/*****************************************************************************
Prototype : FullQueue
Description : 检查队列
Input : PQUEUE Q
Output : None
History :
1.Date : 2016/12/6
Author : Pan
Modification : Created function
*****************************************************************************/
bool FullQueue(PQUEUE Q)
{
if(Q->nFront==(Q->nRear+1)%Q->nMaxSize) //判断循环链表是否满,留一个预留空间不用
{
return TRUE;
}
else
{
return FALSE;
}
}
/*****************************************************************************
Prototype : EmptyQueue
Description : 检查队列
Input : PQUEUE Q
Output : None
History :
1.Date : 2016/12/6
Author : Pan
Modification : Created function
*****************************************************************************/
bool EmptyQueue(PQUEUE Q)
{
if(Q->nFront==Q->nRear) //判断是否为空
return TRUE;
else
return FALSE;
}
/*****************************************************************************
Prototype : Enqueue
Description : 入队
Input : PQUEUE Q
QUEUE_DATA_TYPE_STRUCTURE *pBuf
History :
1.Date : 2016/12/6
Author : Pan
Modification : Created function
*****************************************************************************/
bool Enqueue(PQUEUE Q, QUEUE_DATA_TYPE_STRUCTURE *pBuf)
{
if(FullQueue(Q))
{
Report("Queue Full,Do you want to cover\n");
if(g_QueueModeUsed==1){
Q->nFront=0;
Q->nRear=0;
//memset(&Q->pBase[Q->nRear],0x00,sizeof(QUEUE_DATA_TYPE_STRUCTURE));
memcpy(&Q->pBase[Q->nRear],pBuf,sizeof(QUEUE_DATA_TYPE_STRUCTURE));
Q->nRear=(Q->nRear+1)%Q->nMaxSize;
return TRUE;
}
else{
return FALSE;
}
}
else
{
memcpy(&Q->pBase[Q->nRear],pBuf,sizeof(QUEUE_DATA_TYPE_STRUCTURE));
Q->nRear=(Q->nRear+1)%Q->nMaxSize;
return TRUE;
}
}
/*****************************************************************************
Prototype : Dequeue
Description : 出列
Input : PQUEUE Q
QUEUE_DATA_TYPE_STRUCTURE *pBuf
History :
1.Date : 2016/12/6
Author : Pan
Modification : Created function
*****************************************************************************/
bool Dequeue(PQUEUE Q, QUEUE_DATA_TYPE_STRUCTURE *pBuf)
{
if(EmptyQueue(Q))
{
return FALSE;
}
else
{
memcpy(pBuf,&Q->pBase[Q->nFront],sizeof(QUEUE_DATA_TYPE_STRUCTURE));
Q->nFront=(Q->nFront+1)%Q->nMaxSize;
return TRUE;
}
}
/*****************************************************************************
Prototype : ReleaseQueue
Description : 释放
Input : PQUEUE Q
History :
1.Date : 2016/12/6
Author : Pan
Modification : Created function
*****************************************************************************/
void ReleaseQueue(PQUEUE Q)
{
free(Q->pBase);
}
void TraverseQueue(PQUEUE Q)
{
QUEUE_DATA_TYPE_STRUCTURE stDataSpi;
int i=Q->nFront;
Report("The element is in the team:\n");
while(i%Q->nMaxSize!=Q->nRear)
{
memset(&stDataSpi,0x00,sizeof(stDataSpi));
memcpy(&stDataSpi,&Q->pBase[i],sizeof(stDataSpi));
Report("No.%d ,%d ; %s ;",i,stDataSpi.bFlag,stDataSpi.chBuf);
i++;
}
Report("\n");
}
#define QUEUE_MAX_SIZE_BUF_USED 10
int main()
{
QUEUE g_Queue;
Report("-Que demo- \n\r");
CreateQueue(&g_Queue,QUEUE_MAX_SIZE_BUF_USED);
//Report("Start addr=%X\n\r",g_Queue.pBase);
QUEUE_DATA_TYPE_STRUCTURE stData_spi;
int i=0,ret=0;
for(i; i<QUEUE_MAX_SIZE_BUF_USED+1005;i++)
{
memset(&stData_spi,0x00,sizeof(stData_spi));
stData_spi.bFlag=i;
strcpy(stData_spi.chBuf,"DEMO");
ret=Enqueue(&g_Queue, &stData_spi);
Report("ret=%d\n",ret);
}
//TraverseQueue(&g_Queue);
//Report("Release addr=%X\n\r",g_Queue.pBase);
QUEUE_DATA_TYPE_STRUCTURE stRevBuf;
i=0;
for(i; i<QUEUE_MAX_SIZE_BUF_USED;i++)
{
memset(&stRevBuf,0x00,sizeof(stRevBuf));
Dequeue(&g_Queue , &stRevBuf);
Report("No.%d %s ",stRevBuf.bFlag,stRevBuf.chBuf);
}
ReleaseQueue(&g_Queue);
return 0;
}