现在的位置: 首页 > 综合 > 正文

数据结构 舞伴问题

2013年01月25日 ⁄ 综合 ⁄ 共 2092字 ⁄ 字号 评论关闭

#include<stdio.h>

#define QueueSize 20

typedef struct {

   char name[QueueSize];

   char sex;

}Person;

 

typedef struct {

   Person *dancer;

   int front;

   int rear;

   int count;

} CirQueue;

 

void InitQueue(CirQueue *Q)

{

    Q->front=Q->rear=0;

    Q->count=0;

}

 

int QueueEmpty(CirQueue *Q)

{

    return Q->count==0;

}

 

int QueueFull(CirQueue *Q)

{  return Q->count == QueueSize;

}

 

Person QueueFront(CirQueue *Q)

{

    if(QueueEmpty(Q)) printf("The queue is empty./n");

    return Q->dancer[Q->front];

}

 

void EnQueue(CirQueue *Q,Person dancer)

{

    if(QueueFull(Q)) printf("Team full!/n");

    Q->count++;

    Q->dancer[Q->rear]=dancer;

    Q->rear=(Q->rear+1)%QueueSize;

}

 

Person DeQueue(CirQueue *Q)

{

    Person temp;

    if(QueueEmpty(Q))printf("Team empty!/n");

    temp=Q->dancer[Q->front];

    Q->count--;

    Q->front=(Q->front+1)%QueueSize;

    return temp;

}

 

void DancePartners(Person *dancer, int num)

{   int i; 

    Person P;

    CirQueue  Mdancers, Fdancers;

    InitQueue(&Mdancers);

    InitQueue(&Fdancers);

    for(i=0;i<num;i++)  {

      P=dancer[i];

      if(P.sex=='F')  EnQueue(&Fdancers, P);

      else EnQueue(&Mdancers, P);

    }

    printf("The partner mix is:/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("There are %d women in the waiting./n",Fdancers.count);

P=QueueFront(&Fdancers);

printf("The first waiting's woman is:%s./n", P.name);

}

if(!QueueEmpty(&Mdancers)) {

printf("There are %d men in the waiting./n",Mdancers.count-1);

P=QueueFront(&Mdancers);

printf("The first waiting's man is:%s./n", P.name);

}

}

 

int main()

{

    int i,j; 

    Person dancer[QueueSize];

    printf("/n Please enter the number of the dances:");

    scanf("%d",&j);

    while(j<=0)

    {

      printf("Input error, please input again:");

      scanf("%d",&j);

    }  

    for(i=1;i<=j;i++)

    {

      printf("Please input the %d honored person's name :",i);

      scanf("%s",&dancer[i-1].name);

      printf("Please input the %d honored person's sex (F/M):",i);

      scanf("%s",&dancer[i-1].sex);

      while(dancer[i-1].sex!='F'&&dancer[i-1].sex!='M')

      {

          printf("Input error, please input again:");

          scanf("%s",&dancer[i-1].sex);

      }

    }

    DancePartners(dancer,i);

}

抱歉!评论已关闭.