#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 5
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];//只能存储MaxSize-1个元素
int front,rear;
}SqQueue;
void InitSqQuence(SqQueue &Q)
{
Q.rear=Q.front=0;
}
bool IsEmpty(SqQueue Q)
{
if(Q.rear==Q.front)
{
return true;
}
return false;
}
bool EnQueue(SqQueue &Q,ElemType e)
{
if((Q.rear+1)%MaxSize==Q.front)
{
return false;
}
Q.data[Q.rear]=e;
Q.rear=(Q.rear+1)%MaxSize;
return true;
}
bool DeQueue(SqQueue &Q,ElemType &e)
{
if(Q.rear==Q.front)
{
return false;
}
e=Q.data[Q.front];
Q.front=(Q.front+1)%MaxSize;
return true;
}
int main() {//循环队列
SqQueue Q;
bool ret;
ElemType e;//存储出队元素
InitSqQuence(Q);
ret=IsEmpty(Q);
if (ret)
{
printf("Q is empty\n");
} else{
printf("Q is not empty\n");
}
EnQueue(Q,3);
EnQueue(Q,4);
EnQueue(Q,5);
ret=EnQueue(Q,6);
ret=EnQueue(Q,7);
if(ret)
{
printf("EnQueue is successful\n");
} else{
printf("EnQueue is not successful\n");
}
ret=DeQueue(Q,e);
if(ret)
{
printf("DeQueue is successful,is %d\n",e);
} else{
printf("DeQueue is not successful\n");
}
ret=DeQueue(Q,e);
if(ret)
{
printf("DeQueue is successful,is %d\n",e);
} else{
printf("DeQueue is not successful\n");
}
ret=EnQueue(Q,7);
if(ret)
{
printf("EnQueue is successful\n");
} else{
printf("EnQueue is not successful\n");
}
ret=DeQueue(Q,e);
if(ret)
{
printf("DeQueue is successful,is %d\n",e);
} else{
printf("DeQueue is not successful\n");
}
return 0;
}
12-25
750