#include <stdio.h>
#define MAX 5
typedef struct
{
int QueueCache[MAX];
int front;
int rear;
}Queue;
Queue *initQueue(Queue *q)
{
q->front=0;
q->rear=0;
return q;
}
int IsFull(Queue *q)
{
if((q->front+1)%MAX==q->rear)
{
return 1;
}
else
{
return 0;
}
}
int IsEmpty(Queue *q)
{
if(q->front==q->rear)
{
return 1;
}
else
{
return 0;
}
}
int InsertElement(Queue *q,int element)
{
if(IsFull(q))
{
return -1;
}
else
{
q->QueueCache[q->front]=element;
q->front=(q->front+1)%MAX;
return 0;
}
}
int DeleElement(Queue *q,int *element)
{
if(IsEmpty(q))
{
return -1;
}else
{
*element = q->QueueCache[q->rear];
q->rear=(q->rear+1)%MAX;
return 0;
}
}
int main()
{
Queue *q;
int element=0;
q = (Queue *)malloc(sizeof(Queue));
q=initQueue(q);
for(int i=0;i<10;i++)
{
if(InsertElement(q,i)==-1)
{
break;
}
}
for(int i=0;i<2;i++)
{
if(DeleElement(q,&element)==-1)
{
printf("Empty\n");
}else
{
printf("%d\n",element);
}
}
printf("insert a element\n");
if(InsertElement(q,45)==-1)
{
printf("Full\n");
}
if(InsertElement(q,56)==-1)
{
printf("Full\n");
}
for(int i=0;i<4;i++)
{
if(DeleElement(q,&element)==-1)
{
printf("Empty\n");
}else
{
printf("%d\n",element);
}
}
free(q);
}
#define MAX 5
typedef struct
{
int QueueCache[MAX];
int front;
int rear;
}Queue;
Queue *initQueue(Queue *q)
{
q->front=0;
q->rear=0;
return q;
}
int IsFull(Queue *q)
{
if((q->front+1)%MAX==q->rear)
{
return 1;
}
else
{
return 0;
}
}
int IsEmpty(Queue *q)
{
if(q->front==q->rear)
{
return 1;
}
else
{
return 0;
}
}
int InsertElement(Queue *q,int element)
{
if(IsFull(q))
{
return -1;
}
else
{
q->QueueCache[q->front]=element;
q->front=(q->front+1)%MAX;
return 0;
}
}
int DeleElement(Queue *q,int *element)
{
if(IsEmpty(q))
{
return -1;
}else
{
*element = q->QueueCache[q->rear];
q->rear=(q->rear+1)%MAX;
return 0;
}
}
int main()
{
Queue *q;
int element=0;
q = (Queue *)malloc(sizeof(Queue));
q=initQueue(q);
for(int i=0;i<10;i++)
{
if(InsertElement(q,i)==-1)
{
break;
}
}
for(int i=0;i<2;i++)
{
if(DeleElement(q,&element)==-1)
{
printf("Empty\n");
}else
{
printf("%d\n",element);
}
}
printf("insert a element\n");
if(InsertElement(q,45)==-1)
{
printf("Full\n");
}
if(InsertElement(q,56)==-1)
{
printf("Full\n");
}
for(int i=0;i<4;i++)
{
if(DeleElement(q,&element)==-1)
{
printf("Empty\n");
}else
{
printf("%d\n",element);
}
}
free(q);
}