#include"stdio.h"
#include"malloc.h"
#define MAX 7
typedef struct{
int queue[MAX];
int front;
int rear;
}Queue;
void InitQueue(Queue *&q)
{
q = (Queue *)malloc(sizeof(Queue));
q->front = q->rear = 0;
}
int isFull(Queue *q)
{
if((q->rear+1)%MAX==q->front)
return 1;
return 0;
}
int EnterQueue(Queue *q, int x)
{
if(isFull(q))
return 0;
q->rear = (q->rear+1)%MAX;
q->queue[q->rear] = x;
return 1;
}
int DeleteQueue(Queue *q, int &x)
{
if(q->rear==q->front)
return 0;
x = q->queue[(q->front+1)%MAX];
q->front = (q->front+1)%MAX;
return 1;
}
int QueueSize(Queue *q)
{
return (q->rear-q->front+MAX)%MAX;
}
void display(Queue *q)
{
int n = q->front+1;
printf("The List: ");
while(n!=(q->rear+1)%MAX)
{
printf("%d ",q->queue[n]);
n = (n+1)%MAX;
}
printf("\n");
}
void main()
{
Queue *q;
InitQueue(q);
for(int i=1;i<5;i++)
EnterQueue(q,i);
display(q);
printf("The queue size is %d \n",QueueSize(q));
int x;
DeleteQueue(q,x);
printf("The delete the element is %d \n",x);
display(q);
printf("The queue size is %d \n",QueueSize(q));
EnterQueue(q,9);
display(q);
printf("The queue size is %d \n",QueueSize(q));
}
循环队列
最新推荐文章于 2022-02-23 15:02:25 发布