当前位置: 首页> 财经> 产业 > 数据结构舞伴问题(C语言代码)

数据结构舞伴问题(C语言代码)

时间:2025/8/27 4:38:34来源:https://blog.csdn.net/2305_78057683/article/details/141425694 浏览次数:0次
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MAXSIZE 100
typedef struct
{char name[20];char sex;
}Person;
typedef struct
{Person* base;//Person类型的数组存储男女信息int front;int rear;
}Queue;
void QueueInit(Queue* ps)
{ps->base = (Person*)malloc(sizeof(Person) * MAXSIZE);if (ps->base == NULL){perror("error");exit(1);}ps->front = ps->rear = 0;
}
void EnQueue(Queue* ps, Person p)
{if ((ps->rear+1)%MAXSIZE == ps->front){printf("队满,无法插入\n");exit(1);}ps->base[ps->rear] = p;ps->rear = (ps->rear + 1) % MAXSIZE;
}
bool QueueEmpty(Queue* ps)
{return ps->front == ps->rear;
}
void QueuePop(Queue* ps,Person *e)
{if (ps->front == ps->rear){printf("队空,无内容可以出队\n");exit(1);}*e = ps->base[ps->front];ps->front = (ps->front + 1) % MAXSIZE;
}
Person Gethead(Queue* ps)
{if (ps->front != ps->rear){return ps->base[ps->front];}
}
void DancePartner(Person dancer[], int num)
{Queue Fsex, Msex;//定义两个Queue类型QueueInit(&Fsex);QueueInit(&Msex);int i;Person p;for (i = 0; i < num; i++){p = dancer[i];if (p.sex == 'F'){//F插入女生队列EnQueue(&Fsex, p);}else{EnQueue(&Msex, p);}}while (!QueueEmpty(&Fsex) && !QueueEmpty(&Msex)){//判断都不为空的话,就男女各自出一人QueuePop(&Fsex, &p);printf("%s ", p.name);QueuePop(&Msex, &p);printf("%s\n", p.name);}if (!QueueEmpty(&Fsex)){//万一是奇数的跳舞者,那么就输出这个名字p = Gethead(&Fsex);printf("多出的跳舞这名字为%s", p.name);}else if (!QueueEmpty(&Msex)){p = Gethead(&Msex);printf("多出的跳舞这名字为%s", p.name);}
}
int main()
{int i, num;Person dancer[MAXSIZE];printf("请输入跳舞人数\n");scanf("%d", &num);printf("请输入跳舞人的名字和性别,(女性别用F,男性别用M标识)\n");for (i = 0; i < num; i++){printf("请输入第%d个跳舞者的信息\n", i + 1);scanf("%s %c", &dancer[i].name, &dancer[i].sex);}DancePartner(dancer, num);return 0;
}

关键字:数据结构舞伴问题(C语言代码)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: