#include <iostream> #include <cstring> using namespace std;
#define MAXQSIZE #define OK 1 #define ERROR 0 #define OVERFLOW-2 typedef int STATUS;
typedef struct{CHAR NAME[20];
char sex;
}person;
typedef person QELEMTYPE;
typedef struct{Qelemtype *base;//when initialized dynamically allocate storage space int front;//head pointer int rear;//tail pointer}sqqueue;
Algorithm 3.13 The initialization Status of the Loop queue Initqueue (Sqqueue &q) {//Constructs an empty queue Q q.base = new Qelemtype[maxqsize]; if (!
Q.base) {return OVERFLOW;//Storage allocation failure} Q.front = 0;
q.rear = 0;
return OK;
///algorithm 3.14 for the length of the loop queue int queuelength (Sqqueue q) {//returns the number of elements of Q, that is, the length of the queue return (q.rear-q.front+maxqsize)%maxqsize;} Algorithm 3.15 the queue Status EnQueue (Sqqueue &q,qelemtype e) {//Insert element E to Q's new team tail element if ((q.rear+1)%maxqsize = = Q.front) {RE
The turn error;//tail pointer adds 1 to the circular meaning and equals the head pointer, indicating that the team is full} q.base[q.rear] = e;
Q.rear = (q.rear+1)%maxqsize;
return OK;
}//algorithm 3.16 loop queue out of the team Status dequeue (sqqueue &q,qelemtype &e) {if (q.rear = = Q.front) {return ERROR;
} e = Q.base[q.front]; Q.Front = (q.front+1)%maxqsize;
return OK;
Status findqueue (sqqueue &q,qelemtype &e)//found the team first person does not team {if (q.rear = Q.front) {return ERROR;
} e = Q.base[q.front];
return OK;
int main () {Sqqueue mdancers,fdancers;
Qelemtype temp;
int choose;
Char Na[20],se;
The basic operation and realization of cout<< "****************** partner matching ******************" <<endl;
cout<< "1 initialization queue" <<endl;
cout<< "2. Insert Dancer" <<endl;
cout<< "3. Distribution End" <<endl;
cout<< "0. Exit" <<endl;
cout<< "************************ I am the dividing line ************************" <<endl<<endl;
Choose=-1;
while (choose) {cout<< "Please enter your choice:";
cin>>choose;
Switch (choose) {case 0:break; Case 1:if (Initqueue (mdancers) &&initqueue (fdancers)) cout<< "initialization succeeded.
"<<endl<<endl;
Break
Case 2:cout<< "Please enter the name and sex:"; Cin>>na>>sE
strcpy (Temp.name,na);
Temp.sex=se; if (se== ' F ') if (EnQueue (fdancers,temp)) cout<< "Insert succeeded.
"<<endl<<endl; else cout<< "Insert failed.
"<<endl<<endl; else if (EnQueue (mdancers,temp)) cout<< "Insert succeeded.
"<<endl<<endl; else cout<< "Insert failed.
"<<endl<<endl;
Break
Case 3:while (Queuelength (fdancers) &&queuelength (mdancers))//Determines whether the pair can be paired with {dequeue (fdancers,temp);
Dequeue (mdancers,temp);
} if (Queuelength (fdancers)) {cout<< "The first person to get a dance partner at the beginning of the next round is:";
Findqueue (fdancers,temp);
cout<<temp.name<< "" <<temp.sex<<endl<<endl;
else if (Queuelength (mdancers)) {findqueue (mdancers,temp);
cout<< "The first person to get a dance partner at the beginning of the next round is:";
Dequeue (mdancers,temp);
cout<<temp.name<< "" <<temp.sex<<endl<<endl; Else cout<< "Perfect team!" <<endl<≪endl;
Break default:cout<< "Invalid input!"
<<endl<<endl;
} return 0; }