#define _QUEUE_DEFAULT_SIAE 8
typedef struct CycleSeqQueue{
ElemType *base;
int capacity;
int front;
int rear;
}CycleSeqQueue;
void CycleSeqQueueInit(CycleSeqQueue *pcq, int sz = _QUEUE_DEFAULT_SIZE);
void CycleSeqQueueShow(CycleSeqQueue *pcq);
void CycleSeqQueueEn(CycleSeqQueue *pcq,ElemType x);
void CycleSeqQueueDe(CycleSeqQueue *pcq);
void CycleSeqQueueDestroy(CycleSeqQueue *pcq);
bool CycleSeqQueueEmpty(CycleSeqQueue *pcq);
bool CycleSeqQueueFull(CycleSeqQueue *pcq);
void CycleSeqQueueFront(CycleSeqQueue *pcq);
void CycleSeqQueueInit(CycleSeqQueue *pcq, int sz)
{
assert(pcq != NULL);
pcq->capacity = sz > _QUEUE_DEFAULT_SIZE ? sz : _QUEUE_DEFAULT_SIZE;
pcq->base = (ElemType*)malloc(sizeof(ElemType)*(pcq->capacity+1));
pcq->front = pcq->rear = 0;
}
void CycleSeqQueueShow(CycleSeqQueue *pcq)
{
assert(pcq != NULL);
assert(pcq != NULL);
int i = 0;
for (i = pcq->front; i !=pcq->rear;)
{
printf("%d ", pcq->base[i]);
i = (i + 1) % (pcq->capacity+1);
}
printf("
");
}
void CycleSeqQueueEn(CycleSeqQueue *pcq,ElemType x)
{
assert(pcq != NULL);
if (CycleSeqQueueFull(pcq))
{
printf(" ,%d .
",x);
return;
}
pcq->base[pcq->rear] = x;
pcq->rear = (pcq->rear + 1) % (pcq->capacity + 1);
}
void CycleSeqQueueDe(CycleSeqQueue *pcq)
{
assert(pcq != NULL);
if (CycleSeqQueueEmpty(pcq))
{
printf(" , ");
}
pcq->front = (pcq->front + 1) % (pcq->capacity + 1);
}
bool CycleSeqQueueFull(CycleSeqQueue *pcq)
{
assert(pcq != NULL);
return (pcq->rear + 1) % (pcq->capacity + 1) == pcq->front;
}
bool CycleSeqQueueEmpty(CycleSeqQueue *pcq)
{
assert(pcq != NULL);
return pcq->front == pcq->rear;
}
ElemType CycleSeqQueueSize(CycleSeqQueue *pcq)
{
assert(pcq != NULL);
size_t size = 0;
size_t i= 0;
for (i = pcq->front; i != pcq->rear; )
{
size++;
i = (i + 1) % (pcq->capacity + 1);
}
return size;
}
void CycleSeqQueueDestroy(CycleSeqQueue *pcq)
{
assert(pcq != NULL);
free(pcq->base);
pcq->base = NULL;
pcq->front = pcq->rear =pcq->capacity= 0;
}
#endif