队列中的舞伴问题,不知道哪里出现了错误,求指点迷津
#include <stdio.h>#include<stdlib.h>
#define queuesize 10
typedef struct{
char name[20];
char sex;
}Person;
typedef Person Datatype;
typedef struct
{
Datatype data[10];
int front,rear,count;
}CirQueue;
CirQueue *InitQueue()
{
CirQueue *q;
q=malloc(sizeof(CirQueue));
q->front=q->rear=0;
q->count=0;
return q;
}
int EnQueue(CirQueue *q,Datatype e)
{
if((q->rear+1)%queuesize==q->front)
{printf("队满");
return -1;
}
else
{ q->count++;
if(q->front==q->rear)
q->data[q->front]=q->data[q->rear]=e;
else{
q->rear=(q->rear+1)%queuesize;
q->data[q->rear]=e;
}
return 1;
}
}
int QueueEmpty(CirQueue *q)
{if(q->front==q->rear) return 1;
else return 0;
}
Datatype DeQueue(CirQueue *q)
{Datatype e;
e=q->data[q->front];
q->front=(q->front+1)%queuesize;
q->count--;
return e;
}
Datatype QueueFront(CirQueue *q)
{if(QueueEmpty(q)!=0)
printf("队空");
return q->data[q->front];
}
void dancePartners(Person dancer[],int num)
{
int i;
Person p;
CirQueue *Mdancers;
CirQueue *Fdancers;
Mdancers=InitQueue();
Fdancers=InitQueue();
for(i=0;i<num;i++){
p=dancer[i];
if(p.sex=='m')
EnQueue(Mdancers,p);
if(p.sex=='f')
EnQueue(Fdancers,p);
}
printf("The dancing partners are:\n");
while(QueueEmpty(Fdancers)==0&&QueueEmpty(Mdancers)==0)
{
p=DeQueue(Fdancers);
printf("%s",p.name);
p=DeQueue(Mdancers);
printf("%s",p.name);
}
if(!QueueEmpty(Fdancers))
{
printf("\n There are %d women waiting for the next round.\n",Fdancers->count);
p=QueueFront(Fdancers);
printf("%s will be the first to get a partener.\n",p.name);
}
else if(!QueueEmpty(Mdancers))
{
printf("\n There are %d men waiting for the next round.\n",Mdancers->count);
p=QueueFront(Mdancers);
printf("%s will be the first to get a partener.\n",p.name);
}
}
void main()
{
Person dancer[50];
int i,num;;
printf("男女总人数为\n");
scanf("%d",&num);
printf("输入舞者的姓名和性别\n");
for(i=0;i<num;i++)
scanf("%s %c",dancer[i].name,&dancer[i].sex);
dancePartners(dancer,num);
}