#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("\n");
}
void CycleSeqQueueEn(CycleSeqQueue *pcq,ElemType x)
{
assert(pcq != NULL);
if (CycleSeqQueueFull(pcq))
{
printf("队列已满,%d不能入队.\n",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
C语言——循环队列源代码
原创
©著作权归作者所有:来自51CTO博客作者mb61cf0b2195f96的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章