#include<iostream>
#include<stdlib.h>
#define MaxSize 50
using namespace std;
typedef int ElemType;
//定义
typedef struct{
ElemType data[MaxSize];
int front,rear;
}SqQueue;
//初始化
void InitQueue(SqQueue &Q)
{
Q.front=Q.rear=0;
}
//判断队列是否为空
bool QueueEmpty(SqQueue Q)
{
if(Q.front==Q.rear)
return true;
else
return false;
}
//元素入队
bool EnQueue(SqQueue &Q,ElemType x)
{
if((Q.rear+1)%MaxSize==Q.front)
{
return false;
}//队满
Q.data[Q.rear] = x;
Q.rear=(Q.rear+1)%MaxSize;
return true;
}
//元素出队
bool DeQueue(SqQueue &Q,ElemType &x)
{
if(Q.rear==Q.front)
{
return false;
}//队空
x=Q.data[Q.front];
Q.front=(Q.front+1)%MaxSize;
return true;
}
int main()
{
SqQueue hahaha;
InitQueue(hahaha);
if(QueueEmpty(hahaha))
cout<<"queue is empty"<<endl;
else
cout<<"queue is not empty"<<endl;
//试验入队
for(int i = 0;i<=5;i++)
{
EnQueue(hahaha,i);
}
for(int i = 0;i<=5;i++)
{
cout<<hahaha.data[i];
}
if(QueueEmpty(hahaha))
cout<<"queue is empty"<<endl;
else
cout<<"queue is not empty"<<endl;
//试验出队,展示过程
for(int i = 0;i<=5;i++)
{
DeQueue(hahaha,i);//调用出队
int cnt = (hahaha.rear-hahaha.front+MaxSize)%MaxSize;//求个数
int num = hahaha.front;//记录队头
while(cnt)//个数就是输出次数
{
cout<<hahaha.data[num]; //输出队头
cnt--;//次数每次减一,cnt=0时退出while
num++;//向后移,输出
}
cout<<endl;
}
if(QueueEmpty(hahaha))
cout<<"queue is empty"<<endl;
else
cout<<"queue is not empty"<<endl;
}
效果图如下: