#include<malloc.h>
#include<stdio.h>
#define TOTAL_SPACE 5
typedef struct CircleIntQueue
{
int data[TOTAL_SPACE];
int front;
int rear;
}*CircleIntQueuePtr;
CircleIntQueuePtr initQueue()
{
CircleIntQueuePtr resultPtr=(CircleIntQueuePtr)malloc(sizeof(CircleIntQueue));
resultPtr->front=resultPtr->rear=0;
return resultPtr;
}
void enqueue(CircleIntQueuePtr paraPtr,int paraValue)
{
printf("enqueue %d\r\n",paraValue);
if((paraPtr->rear+1)%TOTAL_SPACE==paraPtr->front)
{
printf("Queue full.\r\n");
return;
}
paraPtr->data[paraPtr->rear]=paraValue;
paraPtr->rear=(paraPtr->rear+1)%TOTAL_SPACE;
}
int dequeue(CircleIntQueuePtr paraPtr)
{
int resultValue;
if(paraPtr->front==paraPtr->rear)
{
printf("No element in queue.\r\n");
return -1;
}
resultValue=paraPtr->data[paraPtr->front];
paraPtr->front=(paraPtr->front+1)%TOTAL_SPACE;
return resultValue;
}
void outputCircleIntQueue(CircleIntQueuePtr paraPtr)
{
int i=0;
if(paraPtr->front==paraPtr->rear)
{
printf("empty queue.\r\n");
return;
}
printf("the elements in the queue: ");
for(i=paraPtr->front;i!=paraPtr->rear;i=(i+1)%TOTAL_SPACE)
{
printf("data[%d] = %d ,",i,paraPtr->data[i]);
}
printf("\r\n");
}
void test()
{
int i=10;
CircleIntQueuePtr tempPtr=initQueue();
for(;i<16;i++)
{
enqueue(tempPtr,i);
}
outputCircleIntQueue(tempPtr);
for(i=0;i<6;i++)
{
printf("dequeue gets %d \r\n",dequeue(tempPtr));
}
for(i=3;i<6;i++)
{
enqueue(tempPtr,i);
}
for(i=20;i<30;i++)
{
enqueue(tempPtr,i);
printf("dequeue gets %d \r\n",dequeue(tempPtr));
outputCircleIntQueue(tempPtr);
}
}
int main()
{
test();
return 1;
}
运行结果: