#include<stdio.h>
#include<malloc.h>
#define Maxsize 100
typedef int dataType ;
typedef struct {
dataType data[Maxsize];
int front,rear;
}Sequeue;
//创建队列
Sequeue *createQueue(){
Sequeue *q = (Sequeue *)malloc(sizeof(Sequeue));
q->front = q->rear = 0;
return q;
}
//判断队列是否为空
int empty(Sequeue *q){
return q->front == q->rear;
}
//判断队列是否为满
int full(Sequeue *q){
return (q->rear+1)%Maxsize == q->front;
}
//元素x进队列
void push(Sequeue *q,dataType x){
if(full(q)) exit(1);
q->data[q->rear] = x;
q->rear = (q->rear+1)%Maxsize;
}
//出队列
void pop(Sequeue *q){
if(empty(q)) exit(1);
q->front = (q->front+1)%Maxsize;
}
//取队头元素的值
dataType front(Sequeue *q){
if(empty(q)) exit(1);
return q->data[q->front];
}
//取队尾元素
dataType rear(Sequeue *q){
if(empty(q)) exit(1);
int k = (q->rear-1+Maxsize)%Maxsize;
return q->data[k];
}
//计算队列元素个数
int size(Sequeue *q){
return (q->rear - q->front + Maxsize)%Maxsize;
}
int main()
{
Sequeue *q = createQueue();
push(q,2);
push(q,10);
push(q,5);
push(q,4);
push(q,6);
pop(q);
printf("队首元素为:%d,队尾元素为:%d \n",front(q),rear(q));
printf("队列元素有%d个 ",size(q));
system("color f0");
}
循环队列
最新推荐文章于 2024-06-17 00:00:00 发布