#include <stdio.h>
#include <assert.h>
#define MaxSize 8
typedef int DataType;
typedef struct {
DataType data[MaxSize];
int front,rear;
}Queue;
void InitQueue(Queue *queue)
{
queue->front=queue->rear=0;
}
int EmptyQueue(Queue *queue)
{
if(queue->rear==queue->front) return 1;
else return 0;
}
int IsFull(Queue *queue)
{
if((queue->rear+1)%MaxSize == queue->front)
{
return 1;
}
return 0;
}
int InQueue(Queue *queue, DataType x)
{
if(IsFull(queue))
{
printf("队列已经满啦!\n");
return 0;
}
queue->data[queue->rear]=x;
queue->rear=(queue->rear+1)%MaxSize;
return 1;
}
int OutQueue(Queue *queue, DataType *x)
{
if(EmptyQueue(queue))
{
printf("队列是空的!!!\n");
return 0;
}
*x=queue->data[queue->front];
queue->front=(queue->front+1)%MaxSize;
return 1;
}
int GetFront(Queue *queue, DataType *data)
{
if(IsFull(queue))
{
printf("队列为空!\n");
return 0;
}
*data = queue->data[queue->front];
return *data;
}
void ClearQueue(Queue *queue)
{
queue->front=queue->rear=0;
}
void PrintQueue(Queue *queue)
{
assert(queue);
int i = queue->front;
while(i!=queue->rear)
{
printf("%-3d的下标是:%-3d \n", queue->data[i], i);
i=(i+1)%MaxSize;
}
printf("\n");
}
int main () {
Queue queue;
DataType data;
InitQueue(&queue);
InQueue(&queue, 1);
InQueue(&queue, 2);
InQueue(&queue, 3);
InQueue(&queue, 4);
InQueue(&queue, 5);
InQueue(&queue, 6);
InQueue(&queue, 7);
printf("队列中的元素为:\n");
PrintQueue(&queue);
printf("\n");
printf("8开始入队!\n");
InQueue(&queue, 8);
OutQueue(&queue, &data);
printf("此次出队列的数据为:%d \n", data);
printf("队列中的元素为:\n");
PrintQueue(&queue);
printf("\n");
OutQueue(&queue, &data);
printf("此次出队列的数据为:%d \n", data);
printf("队列中的元素为:\n");
PrintQueue(&queue);
printf("\n");
InQueue(&queue, 10);
printf("队列中的元素为:\n");
PrintQueue(&queue);
printf("\n");
InQueue(&queue, 11);
printf("队列中的元素为:\n");
PrintQueue(&queue);
printf("\n");
printf("13开始入队!\n");
InQueue(&queue,13);
return 0;
}