#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int QElemType;
typedef int Status;
typedef struct{
QElemType *base;
int front;//尾指针
int rear;//头指针
}SqQueue;
Status InitQueue(SqQueue &Q){
Q.base=new QElemType[MAXSIZE];
if(!Q.base)
return OVERFLOW;//存储失败
Q.front=0;//尾指针初始化
Q.rear=0;//头指针初始化
return OK;
}
Status QueueLength(SqQueue S){
return (S.rear-S.front+MAXSIZE)%MAXSIZE;//求队列的长度
}
Status EnQueue(SqQueue &S,int e){//往队列里边进数
if((S.rear+1)%MAXSIZE==S.front)//当头指针加1与尾指针相同时,队满
return ERROR;
S.base[S.rear]=e;//数据进队列
S.rear=(S.rear+1)%MAXSIZE;//头指针加1
return OK;
}
Status DeQueue(SqQueue &S,QElemType &e){
if(S.rear==S.front)
return ERROR;
e=S.base[S.front];
S.front=(S.front+1)%MAXSIZE;
return OK;
}
int main(){
SqQueue S;
QElemType m;
int i,j,k,t,n;
if(InitQueue(S)==OK){
cout<<"进循环队列的数字依次为:"<<endl<<endl;
for(j=1;j<=13;j++){
EnQueue(S,j);
cout<<j<<" ";
}
cout<<endl;
cout<<"循环队列的长度为"<<QueueLength(S)<<endl;
cout<<"出循环队列的次序依次为:"<<endl<<endl;
while(DeQueue(S,m)==OK){
cout<<m<<" ";
}
}
return 0;
}
队列的简单的操作
最新推荐文章于 2020-10-16 21:15:56 发布