C语言实现的循环队列,用数组形式实现的,下面是实现的代码,初始化,插入,删除,和打印操作。
#include<stdlib.h>
#include"seq_queue.h"
#include <stdio.h>
#include<math.h>
static bool is_full(seq_queue *q)
{
return (q->tail+1)%MAX_SIZE == q->head;
}
bool is_empty(seq_queue *q)
{
return q->head == q->tail;
}
bool init_queue(seq_queue *q)
{
if(q==NULL)
{
return false;
}
q->head=0;
q->tail=0;
return true;
}
bool push_queue(seq_queue *q,ELEM_TYPE e)
{
if(is_full(q))
{
printf("队列已满!\n");
return false;
}
q->data[q->tail]=e;
q->tail=(q->tail+1)%MAX_SIZE;
return true;
}
bool pop_queue(seq_queue *q,ELEM_TYPE *e)
{
if(is_empty(q))
{
printf("队列为空!\n");
return false;
}
*e=q->data[q->head];
q->head=(q->head+1)%MAX_SIZE;
return true;
}
bool show_queue(seq_queue *q)
{
if(q==NULL || is_empty(q))
{
return false;
}
int i=0;
if(q->tail>q->head)
{
for(i=q->head;i<q->tail;i++)
{
printf("%d ",q->data[i]);
}
}
else if(q->tail<q->head)
{
for(i=q->head;i<MAX_SIZE;i++)
{
printf("%d ",q->data[i]);
}
for(i=0;i<q->tail;i++)
{
printf("%d ",q->data[i]);
}
}
return true;
}