数据结构之数组实现队列
/*
注意事项: if((Q->front)%Maxsize!=Q->rear )而不是 if((Q->front+1)%Maxsize!=Q->rear )
*/
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define ElemType int
#define Maxsize 20
#define OK 1
#define ERROR 0
typedef struct queue
{
int front;
int rear;
ElemType *base;
}queue,*Queue;
int Create_queue(Queue Q)
{
Q->base = (ElemType *)malloc(Maxsize*sizeof(ElemType));
if(!Q->base )
exit(0);
Q->front = Q->rear=0;
return OK;
}
int Enter_queue(Queue Q,int item)
{
if((Q->rear +1)%Maxsize!=Q->front )
Q->base [Q->rear ] = item;
++Q->rear ;
return OK;
}
int pop_queue(Queue Q)
{
if((Q->front)%Maxsize!=Q->rear )
printf("%d\n",Q->base [Q->front]);
++Q->front ;
return OK;
}
int main()
{
queue Q;
Create_queue(&Q);
for(int i=0;i<19;++i)
Enter_queue(&Q,i);
//Enter_queue(&Q,2);
// Enter_queue(&Q,3);
for( i=0;i<19;++i)
pop_queue(&Q);
//pop_queue(&Q);
//pop_queue(&Q);
return 0;
}