用顺序队列实现的舞会舞伴选择问题

#define QueneSize 100
/*
  先入队的男士和女士优先配成舞伴
  在 算法中 将男士和女士的记录存放再一个数组中作为输入,然后依次扫描该数组的各个元素
  。并且根据性别决定是进入男队还是女队。当两个队列构造完成后,依次将两队列的对头元素出队构成舞伴,直到某个队列为空为止。
*/
#include<stdlib.h>
#include<stdio.h>

typedef struct 
{
char name[20];//用于存放舞伴的名字
char sex;
}Person;
  typedef Person datatype;
typedef struct 
{
int font;
int rear;
int count; //计数器,用于计数队列中元素的总数
    datatype data[QueneSize];  
}CirQuene;
typedef Person DataType;//将队列中元素的数据类型设置为Person类型
/*
 首先将所有的人按照性别建立2个队列,一个是男士队列,一个是女士队列,只要两个队列不为空,则依次取出
 两个队列的队首元素配成舞伴,若其中任一个队列为空,则告知非空队列中人数和队首人员的名字。

*/

 void initilize(CirQuene *Q)
{
Q->font=0;
Q->rear=0;
Q->count=0;
}

//判断队列满
int QueneFull(CirQuene *Q)
{
return  Q->count==QueneSize;
}
 int QueneEmPty(CirQuene *Q)
 {
return Q->count==QueneSize;
 }
 //入队
 void EnQuene(CirQuene *Q,datatype x)
 {
    if(QueneFull(Q))
{
     printf(“Quene over flow”);
}
Q->count++;
Q->data[Q->rear]=x;
Q->rear=(Q->rear+1)%QueneSize; //循环意义下将尾指针+1
 }
 //出队
 datatype DeQuene(CirQuene *Q)
 {
      datatype temp;
 
   //datatype temp;
   if(QueneEmPty(Q))
   {
     printf(“Quene underflow”);  //栈区下溢
   }
   temp=Q->data[Q->font]; //元素先出栈
   Q->count–; 
   //更新头指针
   Q->font=(Q->font+1)%QueneSize;
   return temp;
 }
bool QueneEmpty(CirQuene *Q)
{
  return Q->count==0;
}
 datatype get_front(CirQuene *Q)
 {
   if(QueneEmpty(Q))
   {
     printf(“Quene is empty”);
   }
   return Q->data[Q->font];
 }
 void DispLay_Quene(CirQuene Q)
 { int i,j;
if(Q.font==Q.rear)
{
printf(“队列为空:!!!”);
}
else
   {
      j = Q.font;
        printf(“出队列的顺序为:\n”);
        for(i = 0;i < (Q.rear – Q.font + QueneSize) % QueneSize;i++)
        {
            printf(“the %d value is %c\n”,i + 1,Q.data[j]);
            j++;
        }    
   }
 }
     datatype QueneFront(CirQuene *Q)
{
          if(QueneEmpty(Q))
 {
 printf(“Quene if empty”);
 }
 return Q->data[Q->font];
}
/*
 首先将所有的人按照性别建立2个队列,一个是男士队列,一个是女士队列,只要两个队列不为空,则依次取出
 两个队列的队首元素配成舞伴,若其中任一个队列为空,则告知非空队列中人数和队首人员的名字。

*/
void DancePartner(Person  dancer [],int num)
{
  //dancer[]用于存放跳舞的男女,num是跳舞的人数
int i;
Person p;
CirQuene Manders,Wdancees;
initilize(&Manders);
    initilize(&Wdancees);
for(i=0;i<num;i++)
{
//依次将跳舞者纳入其性别队列
p=dancer[i];
if(p.sex==’F’)
{
              EnQuene(&Wdancees,p);
}
else
{
EnQuene(&Manders,p);
}
      
}
printf(“The dancers pathers are: \n”);
while(!QueneEmpty(&Manders)&&!QueneEmpty(&Wdancees))
{
//依次输入男女舞伴的名字
p=DeQuene(&Wdancees);
printf(“女舞伴的名字是:  %s”,p.name);
p=DeQuene(&Manders);
printf(” 男舞伴的名字是:%s\n”,p.name);
}
if(!(Wdancees.count==0))
{
      printf(“there are %d women wait in for the next round.\n”,Wdancees.count);
 p=QueneFront(&Wdancees);//取队头
 printf(“%s will be the first to get a parther”,p.name);
}
else if(!(Manders.count==0))
{
       printf(“there are %d man wait in for the next round.\n”,Manders.count);
  p=QueneFront(&Manders);//取队头
  printf(“%s will be the first to get a parther\n”,p.name);
}

}
 int main()
 { 
   //初始化结构体数组
Person dancers[]={
 
{“张三”,’M’},
{“Mary”,’F’},
{“秀秀”,’F’},
{“黎明”,’M’},
{“成龙”,’M’},
{“婷婷”,’F’},
{“佳佳”,’F’},
{“花花”,’F’},
{“good”,’F’},
{“Ruby”,’M’},
{“huazai”,’M’},
{“雪青”,’F’},
{“libai”,’M’}
 
};

     int num=13;
    DancePartner(dancers,num);
 
   return 0;
 }

    原文作者:舞伴问题
    原文地址: https://blog.csdn.net/qq_26992267/article/details/49639581
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞