#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
typedef struct Queue{
int front;
int rear;
int data[maxsize];
}Queue;
Queue* initQueue()
{
Queue* q=(Queue*)malloc(sizeof(Queue));
q->front=0;
q->rear=0;
}
int isfull(Queue* q)
{
if((q->rear+1)%maxsize==q->front)
{
return 1;
}
else
{
return 0;
}
}
int enqueue(Queue* q,int data)
{
if(isfull(q))
{
return 0;
}
else
{
q->data[q->rear]=data;
q->rear=(q->rear+1)%maxsize;
}
}
int isempty(Queue* q)
{
if(q->front==q->rear)
{
return 1;
}
else
{
return 0;
}
}
int deQueue(Queue* q)
{
if(isempty(q))
{
return -1;
}
else
{
int data=q->data[q->front];
q->front=(q->front+1)%maxsize;
return data;
}
}
void print_Queue(Queue* q)
{
int leng=(q->rear-q->front+maxsize)%maxsize;
int index=q->front;
for(int i=0;i<leng;i++)
{
printf("%d ",q->data[index]);
index=(index+1)%maxsize;
}
}
int main()
{
Queue* q=initQueue();
enqueue(q,1);
enqueue(q,2);
enqueue(q,3);
enqueue(q,4);
print_Queue(q);
printf("\n");
deQueue(q);
print_Queue(q);
return 0;
}
数据结构与算法 循环队列
最新推荐文章于 2024-09-03 18:29:17 发布