隊列的應用
假設在周末舞會上
若兩隊初始人數不相同
先入隊的男士或女士亦先出隊配成舞伴
在算法中
是女隊
對者
typedef struct{
char name[
char sex; //性別
}Person;
typedef Person DataType; //將隊列中元素的數據類型改為Person
void DancePartner(Person dancer[]
{//結構數組dancer中存放跳舞的男女
int i;
Person p;
CirQueue Mdancers
InitQueue(&Mdancers);//男士隊列初始化
InitQueue(&Fdancers);//女士隊列初始化
for(i= p=dancer[i]; if(p.sex=='F') EnQueue(&Fdancers.p); //排入女隊 else EnQueue(&Mdancers.p); //排入男隊 } printf("The dancing partners are: \n \n"); while(!QueueEmpty(&Fdancers)&&!QueueEmpty(&Mdancers)){ //依次輸入男女舞伴名 p=DeQueue(&Fdancers); //女士出隊 printf("%s ",p.name);//打印出隊女士名 p=DeQueue(&Mdancers); //男士出隊 printf("%s\n",p.name); //打印出隊男士名 } if(!QueueEmpty(&Fdancers)){ //輸出女士剩余人數及隊頭女士的名字 printf("\n There are %d women waitin for the next round.\n",Fdancers.count); p=QueueFront(&Fdancers); //取隊頭 printf("%s will be the first to get a partner. \n",p.name); }else if(!QueueEmpty(&Mdancers)){//輸出男隊剩余人數及隊頭者名字 printf("\n There are%d men waiting for the next round.\n",Mdacers.count); p=QueueFront(&Mdancers); printf("%s will be the first to get a partner.\n",p.name); } }//DancerPartners
From:http://tw.wingwit.com/Article/program/sjjg/201311/23917.html