#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define MAX_QUEUE_SIZE 4
typedef int ElemType;
typedef int status;
typedef struct queue
{
ElemType Queue_array[MAX_QUEUE_SIZE];
int front;
int rear;
}SqQueue;
//1.初始化队列
status Init_Queue(SqQueue *Q)
{
if(!Q)
{
printf("初始化失败\n");
return ERROR;
}
Q->rear=Q->front=0;
return OK;
}
//2.入队
status Insert_Queue(SqQueue *Q,ElemType e)
{
if((Q->rear+1)%MAX_QUEUE_SIZE==Q->front)
return ERROR;
Q->Queue_array[Q->rear]=e;
Q->rear=(Q->rear+1)%MAX_QUEUE_SIZE;
return OK;
}
//3.出队
status Delete_CirQueue(SqQueue *Q,ElemType *e)
{
if(Q->front==Q->rear)
return ERROR;
*e=Q->Queue_array[Q->front];
Q->front=(Q->front+1)%MAX_QUEUE_SIZE;
return OK;
}
int main()
{
SqQueue Q;
ElemType e;
Init_Queue(&Q);
printf("请输入数据(按0结束)\n");
while(1)
{
scanf("%d",&e);
if(e==0)
break;
if(!Insert_Queue(&Q,e))
{
exit(0);
}
}
printf("\n");
while(1)
{
if(!Delete_CirQueue(&Q,&e))
{
break;
}
printf("%d ",e);
}
printf("\n");
}