#include
#include
#include
#define true 1
#define false 0
typedef int QueueType;
typedef struct _tQUEUE_
{
QueueType* pValue;
int front;
int rear;
unsigned int totalSize;
unsigned char initFlag;
}MyQueue;
unsigned char QueueIsEmpty(MyQueue* pQueue);
unsigned char QueueIsFull(MyQueue* pQueue);
void InitQueueOnce(MyQueue* pQueue, unsigned int totalSize);
void PopQueue(MyQueue* pQueue, QueueType* value);
void EnterQueueFullAutoPop(MyQueue* pQueue, QueueType value);
void EnterQueue(MyQueue* pQueue, QueueType value);
void QueuePrintAllValue(MyQueue* pQueue);
QueueType getQueueSum(MyQueue* pQueue);
//初始化队列
void InitQueueOnce(MyQueue* pQueue, unsigned int totalSize)
{
if (pQueue->initFlag != 1)
{
pQueue->pValue = (QueueType*)malloc(sizeof(QueueType) * (totalSize + 1));
if (pQueue->pValue != NULL)
{
pQueue->front = pQueue->rear = 0;
}
pQueue->totalSize = totalSize;
pQueue->initFlag = 1;
}
}
//判断队列是否为空
unsigned char QueueIsEmpty(MyQueue* pQueue)
{
if (pQueue->front == pQueue->rear)
{
return true;
}
else
{
return false;
}
}
//判断队列是否满了
unsigned char QueueIsFull(MyQueue* pQueue)
{
if ((pQueue->rear + 1) % (pQueue->totalSize + 1) == pQueue->front)
{
return true;
}
else
{
return false;
}
}
//将值输入队列
void EnterQueue(MyQueue* pQueue, QueueType value)
{
if (QueueIsFull(pQueue) != true)
{
pQueue->pValue[pQueue->rear] = value;
pQueue->rear = (pQueue->rear + 1) % (pQueue->totalSize + 1); //Tail pointer offset
}
}
//将值输入队列,如果队列已经满了,则先将最前的值弹出去,先进先出
void EnterQueueFullAutoPop(MyQueue* pQueue, QueueType value)
{
if (QueueIsFull(pQueue))
{
pQueue->front = (pQueue->front + 1) % (pQueue->totalSize + 1);
}
//Note: cannot use else!
pQueue->pValue[pQueue->rear] = value;
pQueue->rear = (pQueue->rear + 1) % (pQueue->totalSize + 1); //Tail pointer offset
}
//将值弹出队列,先进先出,value为被弹出的值
void PopQueue(MyQueue* pQueue, QueueType* value)
{
if (QueueIsEmpty(pQueue) != true)
{
*value = pQueue->pValue[pQueue->front];
pQueue->front = (pQueue->front + 1) % (pQueue->totalSize + 1);
}
}
//输出队列所有的值
void QueuePrintAllValue(MyQueue* pQueue)
{
int count = 0;
if (QueueIsEmpty(pQueue) != true)
{
int ret = pQueue->front;
while (ret != pQueue->rear)
{
count++;
printf("第%d个为%d ", count, pQueue->pValue[ret]);
ret = (ret + 1) % (pQueue->totalSize + 1);
}
}
}
QueueType getQueueSum(MyQueue* pQueue)
{
QueueType QueueSum = 0;
int ret = 0;
if (QueueIsEmpty(pQueue) != true)
{
ret = pQueue->front;
while (ret != pQueue->rear)
{
QueueSum += pQueue->pValue[ret];
ret = (ret + 1) % (pQueue->totalSize + 1);
}
}
return QueueSum;
}
int main()
{
float val;
FloatQueue m;
InitQueue(&m);
EnterQueueFullAutoPop(&m, 1.1);
EnterQueueFullAutoPop(&m, 2.2);
EnterQueueFullAutoPop(&m, 3.3);
EnterQueueFullAutoPop(&m, 4.4);
QueuePrintAllValue(&m);
printf("\n\n");
EnterQueueFullAutoPop(&m, 5.5);
EnterQueueFullAutoPop(&m, 6.6);
EnterQueueFullAutoPop(&m, 7.7);
EnterQueueFullAutoPop(&m, 8.8);
EnterQueueFullAutoPop(&m, 9.9);
QueuePrintAllValue(&m);
printf("\n\n");
EnterQueueFullAutoPop(&m, 10.1);
EnterQueueFullAutoPop(&m, 11.1);
EnterQueueFullAutoPop(&m, 12.2);
QueuePrintAllValue(&m);
printf("\n\n");
EnterQueueFullAutoPop(&m, 13.3);
EnterQueueFullAutoPop(&m, 14.4);
EnterQueueFullAutoPop(&m, 15.5);
QueuePrintAllValue(&m);
printf("\n\n");
PopQueue(&m, &val);
QueuePrintAllValue(&m);
return 0;
}