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

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值