队列的应用--舞会配对

作者在 2015-07-30 11:42:15 发布以下内容

题目:假设在周末舞会上,男士们和女士们进入舞厅时,各自排成一队。跳舞开始时,一次从男队和女队的队头上各自出一人配成舞伴。若两队初始人数不相同,则较长的那一队中未配对者等待下一轮舞曲。

 

#include <stdio.h> #include <stdlib.h> typedef struct queue   //用链式队列 {     char name;     struct queue *next; }QUEUE; QUEUE *frontM=NULL,*rearM=NULL; QUEUE *frontF=NULL,*rearF=NULL;

void AddQueue(char x,int flag)//添加结点函数 {     QUEUE *s;//新建一个结点     s=(QUEUE*)malloc(sizeof(QUEUE));     s->name=x;     s->next=NULL;     if(flag==1)//性别为男,加入男队列中     {         if(frontM==NULL)             frontM=s;         else             rearM->next=s;         rearM=s;     }     else       //性别为女,加入女队列中     {         if(frontF==NULL)             frontF=s;         else             rearF->next=s;         rearF=s;     } } void DelQueue(char *e,int flag)//删除结点函数,配过对的要删掉 {     QUEUE *frontT;//临时结点     if(flag==1)         frontT=frontM;     else         frontT=frontF;     if(frontT==NULL)         printf("QUEUE is empty!");     else     {         QUEUE *temp;//新建一个临时结点         temp=frontT->next;//用temp记录下位置         *e=frontT->name;         free(frontT);//把传过来的结点删掉         frontT=temp;     }     if(flag==1)         frontM=frontT;//再把此时结点的位置传给头结点     else         frontF=frontT;     return;

} void InitiQueue()//初始化队列函数 {     int flag;//性别标志     int lenF,lenM;     char c;//姓名     c='A';     while(c!='\n'&&c!=NULL)     {         AddQueue(c,1);         c=getchar();     }     c='a';     while(c!='\n'&&c!=NULL)     {         AddQueue(c,0);         c=getchar();     } }

void DisplayQueue(int flag)//显示队列 {     QUEUE *tempr,*tempf;     if(flag==1)     {         tempr=rearM;         tempf=frontM;     }     else     {         tempr=rearF;         tempf=frontF;     }     if(tempr==NULL)     {         printf("Queue is empty\n");         return;     }     while (tempf!=NULL)     {         printf(" %c",tempf->name);         tempf=tempf->next;

    }     printf("\n"); } void Dance() {     int flag;     printf("舞会开始前,初始化队列\n");     InitiQueue();     printf("男队列:");DisplayQueue(1);     printf("女队列:");DisplayQueue(0);     printf("舞会开始\n");     char name;     while(name!='0')     {         printf("一支曲子开始\n");         while(frontM!=NULL && frontF!=NULL)         {             DelQueue(&name,1);             printf("%c",name);             DelQueue(&name,0);             printf("%c",name);         }         printf("\n");         printf("男队列:");DisplayQueue(1);         printf("女队列:");DisplayQueue(0);         printf("一支曲子结束\n");         printf("有人进队,输入性别和姓名\n");         scanf("%d",&flag);name=getchar();         AddQueue(name,flag);         if(rearM->name=='0'||rearF->name=='0')         {             printf("舞会结束,停止进队\n");             break;         }         else         {             printf("有人进队,输入性别和姓名\n");             scanf("%d",&flag);             name=getchar();             AddQueue(name,flag);             printf("男队列:");DisplayQueue(1);             printf("女队列:");DisplayQueue(0);         }     } } void main() {     Dance(); }

数据结构 | 阅读 1643 次
文章评论,共0条
游客请输入验证码