1.链表解决
呜呜,好像不是用队列的链式存储方式来解决的,就是普通链表,没想出来怎么用队列,还不太会,有空的话记得回来看看
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define N 100
typedef struct queue
{
char name[N];
struct queue *next;
}QUEUE;
QUEUE *creatqueue(QUEUE *head,int *number);
void dancepartners(QUEUE *manhead,QUEUE *womenhead,int mannumber,int wonmennumber);
void match(QUEUE *shortq,QUEUE *longq);
void dequeue(QUEUE *q,char *str);
int main(){
QUEUE *manhead=NULL,*wonmenhead=NULL;
int mannumber,wonmennumber;
printf("男队:\n");
manhead=creatqueue(manhead,&mannumber);
printf("女队:\n");
wonmenhead=creatqueue(wonmenhead,&wonmennumber);
dancepartners(manhead,wonmenhead,mannumber,wonmennumber);
return 0;
}
QUEUE *creatqueue(QUEUE *head,int *number){
QUEUE *p,*pt;
p=(QUEUE *)malloc(sizeof(QUEUE));
head=p;
pt=p;
int i;
printf("请输入跳舞人数\n");
scanf("%d",number);
scanf("%s",p->name);
for(i=1;i<*number;i++){
p=(QUEUE *)malloc(sizeof(QUEUE));
pt->next=p;
pt=p;
scanf("%s",p->name);
}
p->next=NULL;
return head;
}
void dancepartners(QUEUE *manhead,QUEUE *womenhead,int mannumber,int wonmennumber){
if(mannumber>wonmennumber){
match(womenhead,manhead);
}else{
match(manhead,womenhead);
}
}
void match(QUEUE *shortq,QUEUE *longq){
int n,t;
QUEUE *p1=shortq;
QUEUE *p2=longq;
char str1[N],str2[N];
printf("请输入舞会轮数:\n");
scanf("%d",&n);
t=n;
while(n--){//先用,后自减1
printf("第%d场:\n",t-n);
while(p1!=NULL){
if(p2==NULL){
p2=longq;
}
dequeue(p1,str1);
dequeue(p2,str2);
printf("配对舞者:%s %s\n",str1,str2);
p1=p1->next;
p2=p2->next;
}
p1=shortq;
if(p2==NULL){
p2=longq;
}
dequeue(p2,str1);
printf("下一场第一个出场的未配对者姓名:%s\n",str1);
}
}
void dequeue(QUEUE *q,char *str){
strcpy(str,q->name);
}
2.队列的顺序存储结构
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define N 100
typedef struct queue
{
char name[N][N];
int qsize;
int front;
int rear;
}QUEUE;
void creatqueue(QUEUE *q);
void dancepartners(QUEUE *man,QUEUE *women);
void match(QUEUE *shortq,QUEUE *longq);
int queueempty(QUEUE *q);
void dequeue(QUEUE *q,char *str);
void getqueue(QUEUE *q,char *str);
int main(){
QUEUE man,wonmen;
printf("男队:\n");
creatqueue(&man);
printf("女队:\n");
creatqueue(&wonmen);
dancepartners(&man,&wonmen);
return 0;
}
void creatqueue(QUEUE *q){
int n,i;
printf("请输入跳舞人数\n");
scanf("%d",&n);
q->front=0;
q->rear=n;
q->qsize=n+1;
for(i=0;i<n;i++){
scanf("%s",q->name[i]);
}
}
void dancepartners(QUEUE *man,QUEUE *women){
if(man->qsize>women->qsize){
match(women,man);
}else{
match(man,women);
}
}
void match(QUEUE *shortq,QUEUE *longq){
int n,t;
char str1[N],str2[N];
printf("请输入舞会轮数:\n");
scanf("%d",&n);
t=n;
while(n--){//先用,后自减1,循环n次
printf("第%d场:\n",t-n);
while(!queueempty(shortq)){//短队列不为空
if(queueempty(longq)){//若长队列为空,长队列从头再轮
longq->front=(longq->front+1)%longq->qsize;
}
dequeue(shortq,str1);
dequeue(longq,str2);
printf("配对舞者:%s %s\n",str1,str2);
}//循环结束时,短队列再次为空
shortq->front=(shortq->front+1)%shortq->qsize;//短队列从头再轮
if(queueempty(longq)){
longq->front=(longq->front+1)%longq->qsize;
}//若内部的while循环结束时,长队列为空,下一个第一个出场的应从头再来
getqueue(longq,str1);
printf("下一场第一个出场的未配对者姓名:%s\n",str1);
}
}
int queueempty(QUEUE *q){
if(q->front==q->rear){
return 1;
}else{
return 0;
}
}
void dequeue(QUEUE *q,char *str){
strcpy(str,q->name[q->front]);
q->front+=1;
}
void getqueue(QUEUE *q,char *str){
strcpy(str,q->name[q->front]);
}