//循环队列程序
#include<stdio.h>
#include<stdlib.h>
#define INIT_QUEUE_SIZE 100
typedef struct{
int *base;
int front;
int rear;
}SqQueue;
SqQueue InitQueue(SqQueue Q);
SqQueue EnQueue(SqQueue Q,int value);
void TraverseQueue(SqQueue Q);
int main(void)
{
SqQueue Q;
int value,i,n;
Q=InitQueue(Q);
printf("Please input element number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&value);
Q=EnQueue(Q,value);
}
TraverseQueue(Q);
free(Q.base);
return 0;
}
SqQueue InitQueue(SqQueue Q)
{
if((Q.base=(int *)malloc(INIT_QUEUE_SIZE * sizeof(int)))==NULL)
{
exit(1);
}
Q.front=0;
Q.rear=0;
return Q;
}
SqQueue EnQueue(SqQueue Q,int value)
{
if((Q.rear+1)%INIT_QUEUE_SIZE == Q.front) //队列满
{
exit(1);
}
Q.base[Q.rear]=value;
Q.rear=(Q.rear+1)%INIT_QUEUE_SIZE; //确定队尾指针的位置
return Q;
}
void TraverseQueue(SqQueue Q)
{
while(Q.front != Q.rear)
{
printf("%d->",Q.base[Q.front++]);
}
printf("null\n");
}