數(shù)據(jù)結(jié)構(gòu)循環(huán)隊(duì)列的代碼實(shí)現(xiàn)2

#include <iostream>
//Queue
#define maxLen 5
using namespace std;
typedef struct elemType {
int elemNo;
string elemName;
}elemType;
typedef struct sqQueue {
elemType data[maxLen];
int front;
int rear;
}sqQueue;
void initQueue(sqQueue &que){
cout<<endl<<"初始化成功!"<<"當(dāng)前隊(duì)列總大小:"<<maxLen-1<<endl;
que.front = 0;
que.rear = 0;
}
void getLength(sqQueue que){
for(int i = 0; i < maxLen; i++){
// cout<<endl<<que.data[i].elemNo<<endl;
}
int len1 = que.rear%maxLen - que.front%maxLen;
int len = 0;
len1>0?len = len1:len = 0 - len1;
cout<<"當(dāng)前隊(duì)列內(nèi)元素個(gè)數(shù)為:"<<len<<endl;
}
void pushQue(sqQueue &que, elemType elem) {//入隊(duì)
if((que.rear+1)%maxLen!=que.front%maxLen){
elem.elemNo=que.rear;
que.data[que.rear++] = elem;
cout<<"入隊(duì)成功!"<<endl;
}
else cout<<"當(dāng)前隊(duì)列已滿?。?#34;<<endl;
}
elemType popQue(sqQueue &que) {//出隊(duì)
elemType elem;
if(que.front%maxLen!=que.rear%maxLen){
cout<<"出隊(duì)元素為:\n";
cout<<que.data[que.front].elemNo<<endl;
elem = que.data[que.front++];
}
else cout<<"當(dāng)前為空隊(duì)列!"<<endl;
return elem;
}
elemType setAndGetEmem(int elemNo, string elemName) {
elemType elem;
elem.elemNo = elemNo;
elem.elemName = elemName;
return elem;
}
void menu (){
cout<<endl<<"\n1.初始化隊(duì)列\(zhòng)n2.入隊(duì)\n3.出隊(duì)\n4.獲取隊(duì)列所有元素\n5.退出"<<endl;
cout<<"請(qǐng)輸入選項(xiàng): "<<endl;
}
int main() {
sqQueue que; //聲明變量
while(1){
menu();
int choice = 1;
cin>>choice;
switch(choice){
case 1:initQueue(que);//初始化
break;
case 2:pushQue(que, setAndGetEmem(1, "test")); //入隊(duì)
break;
case 3:popQue(que); //出隊(duì)
break;
case 4:getLength(que); //獲隊(duì)列元素
break;
case 5:cout<<"已退出??!";return 0;
break;
default:cout<<"輸入有誤!"<<endl;
}
}
return 0;
}