//循环队列的基本操作
#include<stdio.h>
#define MaxSize 50
typedef int ElemType;
//定义循环队列结构体
typedef struct
{
ElemType data[MaxSize];
int front,rear;
}SqQueue;
//初始化
void InitQueue(SqQueue &Q)
{
Q.rear = Q.front = 0;
}
//判断队列是否为空
bool isEmpty(SqQueue &Q)
{
if(Q.rear == Q.front)
return true;
else
return false;
}
//入队操作
bool EnQueue(SqQueue &Q,ElemType x)
{
if((Q.rear+1)%MaxSize == Q.front)
return false;
Q.rear = (Q.rear+1)%MaxSize;
Q.data[Q.rear] = x;
printf("%d\n",Q.data[Q.rear]); //用于显示入队数据
return true;
}
//出队操作
bool DeQueue(SqQueue &Q,ElemType &x)
{
if(Q.rear == Q.front)
return false;
Q.front = (Q.front+1)%MaxSize;
x = Q.data[Q.front];
printf("%d\n",x); //用于显示出队数据
return true;
}
void main()
{
SqQueue q;
int m,x;
InitQueue(q);
EnQueue(q,5);
EnQueue(q,23);
EnQueue(q,31);
EnQueue(q,36);
DeQueue(q,x);
m = isEmpty(q);
printf("%d\n",m);
#include<stdio.h>
#define MaxSize 50
typedef int ElemType;
//定义循环队列结构体
typedef struct
{
ElemType data[MaxSize];
int front,rear;
}SqQueue;
//初始化
void InitQueue(SqQueue &Q)
{
Q.rear = Q.front = 0;
}
//判断队列是否为空
bool isEmpty(SqQueue &Q)
{
if(Q.rear == Q.front)
return true;
else
return false;
}
//入队操作
bool EnQueue(SqQueue &Q,ElemType x)
{
if((Q.rear+1)%MaxSize == Q.front)
return false;
Q.rear = (Q.rear+1)%MaxSize;
Q.data[Q.rear] = x;
printf("%d\n",Q.data[Q.rear]); //用于显示入队数据
return true;
}
//出队操作
bool DeQueue(SqQueue &Q,ElemType &x)
{
if(Q.rear == Q.front)
return false;
Q.front = (Q.front+1)%MaxSize;
x = Q.data[Q.front];
printf("%d\n",x); //用于显示出队数据
return true;
}
void main()
{
SqQueue q;
int m,x;
InitQueue(q);
EnQueue(q,5);
EnQueue(q,23);
EnQueue(q,31);
EnQueue(q,36);
DeQueue(q,x);
m = isEmpty(q);
printf("%d\n",m);
}
运行结果: