写作不易,如可请点赞
打赏就是作者发布最作业的动力
使用结构体
英语不好用的是拼音
#include<iostream>
using namespace std;
typedef struct xuhuandl{
int *data;
int maxSize;
int front,rear;
}xuhuandl,*Xuhuandl;
void chushua(Xuhuandl &dl,int daxiao);//初始化队列
int pankong(Xuhuandl &dl);//检查队列是否为空
void qingkongdl(Xuhuandl &dl);//清空队列
void rudl(Xuhuandl &dl,int shu);//入队
int panduanyid(Xuhuandl &dl);//判断溢出
void chudl(Xuhuandl &dl);//出队
void qudshoyshu(Xuhuandl &dl);//取队首元素
int main(){
int xz;
int shu;
Xuhuandl dl=new xuhuandl;
int daxiao;
for(int i=0;i<100;){
cout<<"1,初始化队列 2,检查队列是否为空"<<"\n";
cout<<"3,清空队列 4,入队"<<"\n";
cout<<"5,出队 6,取队首元素"<<"\n";
cin>>xz;
if(xz==1){
cout<<"请输入要生成的大小"<<"\n";
cin>>daxiao;
chushua(dl,daxiao);
}
if(xz==2){
pankong(dl);
}
if(xz==3){
qingkongdl(dl);
}
if(xz==4){
cout<<"请输入要入队的数"<<"\n";
cin>>shu;
rudl(dl,shu);
}
if(xz==5){
chudl(dl);
}
if(xz==6){
qudshoyshu(dl);
}
}
}
int panduanyid(Xuhuandl &dl){
if((dl->front+1)%dl->maxSize==dl->rear%dl->maxSize){
cout<<"队列已满队列"<<"\n"<<"入队失败"<<"\n";
return 1;
}
return 0;
}
void chushua(Xuhuandl &dl,int daxiao){
dl->maxSize=daxiao;
dl->front=0;
dl->rear=0;
dl->data=new int[dl->maxSize];
cout<<"初始化完成"<<"\n";
}
int pankong(Xuhuandl &dl){
if(dl->rear==dl->front){
cout<<"此时为空"<<"\n";
return 1;
}else{
cout<<"此时为不空"<<"\n";
return 0;
}
}
void qingkongdl(Xuhuandl &dl){
dl->rear=dl->front;
cout<<"已清空"<<"\n";
}
void rudl(Xuhuandl &dl,int shu){
if(panduanyid(dl))return;
dl->data[dl->front]=shu;
dl->front++;
}
void chudl(Xuhuandl &dl){
if(pankong(dl)){
cout<<"无法出队"<<"\n";
}else{
cout<<"出队的数为"<<dl->data[dl->rear]<<"\n";
dl->rear++;
}
}
void qudshoyshu(Xuhuandl &dl){
if(pankong(dl)){
cout<<"无法取队首元素"<<"\n";
}else{
cout<<"取队首元素为"<<dl->data[dl->rear]<<"\n";
}
}