#include<stdio.h>
#include<malloc.h>
#define MaxSize 20
typedef int dataType;
typedef struct {
dataType data[MaxSize];
dataType rear; //尾部
dataType front; //头部
}Queue;
//初始化循环队列
Queue * CreateQueue()
{
Queue *q;
q=(Queue *)malloc(sizeof(Queue));
q->front=q->rear=0;
printf("初始化成功!!!!\n");
return q;
}
//判空
int Empty(Queue *q)
{
if(q->rear!=q->front)
return 1; //队列不为空
return 0; //队列为空
}
//判满
int full(Queue *q)
{
if((q->rear+1)%MaxSize==q->front)
return 1; //队列满
return 0; //队列不满
}
//入队操作
void input(Queue *q,dataType x)
{
if(full(q)) //满
exit(1);
q->data[q->rear]=x; //不满
q->rear=(q->rear+1 )%MaxSize;
printf("入队成功!!!!\n");
}
//出队操作
void output(Queue *q)
{
if(!Empty(q)) //空
exit(1);
printf("出队元素:%d\n",q->data[q->front]);
q->front=(q->front+1)%MaxSize;
}
//取队头元素
int Front(Queue *q)
{
return q->data[q->front];
}
//取队尾元素
int Rear(Queue *q)
{
return q->data[((q->rear-1+MaxSize)%MaxSize)];
}
//元素个数
int Size(Queue *q)
{
int count=(q->rear-q->front+MaxSize)%MaxSize;
return count;
}
//
void main()
{
Queue *head;
head=CreateQueue();
input(head,10);
input(head,20);
input(head,30);
input(head,40);
input(head,50);
input(head,60);
input(head,70);
printf("队尾元素:%d\n",Rear(head));
printf("队头元素:%d\n",Front(head));
printf("元素个数:%d",Size(head));
printf("--------------------------\n");
output(head);
output(head);
output(head);
output(head);
printf("队尾元素:%d\n",Rear(head));
printf("队头元素:%d\n",Front(head));
printf("元素个数:%d",Size(head));
}
希望各位,点赞,加关注!!!!!!!!