回答:这边给出半成品了,写的不好,仅供参考(采用单链表编写,实现循环链表功能,用单链表去实现,效率会降低许多的感觉)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ERROR -1
typedef struct Dancer {
char name[20];
int sex; //性别,1表示男,2表示女
int status; //状态,1表示在跳舞,2表示在等待
struct Dancer* next;
}Dancer;
Dancer* Male_init_head() {
Dancer* head = (Dancer*)malloc(sizeof(Dancer));
strcpy(head->name, "");
head->sex = 1;
head->next = NULL;
return head;
}
Dancer* Female_init_head() {
Dancer* head = (Dancer*)malloc(sizeof(Dancer));
strcpy(head->name, "");
head->sex = 2;
head->next = NULL;
return head;
}
Dancer* Init_head() {
Dancer* head = (Dancer*)malloc(sizeof(Dancer));
strcpy(head->name, "");
head->sex = 2;
head->next = NULL;
return head;
}
/*
这里采用尾插法,符合先来的排在前面的规则
*/
void Init_list(Dancer* head, int num) {
Dancer* current = head;
int i = 0;
while (i < num) {
Dancer* new_node = (Dancer*)malloc(sizeof(Dancer));
scanf("%s", new_node->name);
scanf("%d", &new_node->sex);
new_node->status = 2;
new_node->next = NULL;
while (current->next)
current = current->next;
current->next = new_node;
i++;
}
}
/*
插入新节点,采用尾插法
*/
void Insert_list(Dancer* head, Dancer* new_node) {
Dancer* current = head;
while (current->next)
current = current->next;
current->next = new_node;
}
void Divide_list(Dancer* head, Dancer* Male_head, Dancer* Female_head) {
Dancer* current = head->next;
while (current)
{
if (current->sex == 1)
Insert_list(Male_head, current);
if (current->sex == 2)
Insert_list(Female_head, current);
current = current->next;
}
}
/*
判断某人是否参加了party,参加了则返回1,没参加返回0
*/
int Is_in_party(Dancer* head, char* name) {
Dancer* current = head->next;
while (current)
{
if (strcmp(current->name, name) == 0)
return 1;
current = current->next;
}
return 0;
}
/*
查询某人是否在跳舞,若该人不存在,返回错误代码ERROR,在跳舞返回1,在等待返回0
*/
int Is_dancing(Dancer* head, char* name) {
Dancer* current = head->next;
while (current)
{
if (strcmp(current->name, name) == 0) {
if (current->status == 1)
return 1;
if (current->status == 2)
return 0;
}
current = current->next;
}
return ERROR;
}
int Attend_num(Dancer* head) {
Dancer* current = head->next;
int count = 0;
while (current)
{
count++;
current = current->next;
}
return count;
}
int Get_Male_num(Dancer* Male_head) {
Dancer* current = Male_head->next;
int count = 0;
while (current)
{
count++;
current = current->next;
}
return count;
}
int Get_Female_num(Dancer* Female_head) {
Dancer* current = Female_head->next;
int count = 0;
while (current)
{
count++;
current = current->next;
}
return count;
}
int Get_waitting_num(Dancer* Male_head, Dancer* Female_head) {
Dancer* current = Male_head->next;
int count = 0;
while (current)
{
if(current->status==2)
count++;
current = current->next;
}
current = Female_head->next;
while (current)
{
if (current->status == 2)
count++;
current = current->next;
}
return count;
}
/*
采用循环队列是很好的思想,可以很好的分配每个人匹配的机会,但这边采用类似于操作系统中NFA的思想进行分配,代码可能会有点重复
这边其实对于曲子的分配会比较复杂,但不考虑曲子的时间之类的,默认一个单位时间后,匹配则结束,上一轮的就接在之前没匹配完的后面
很容易可以知道,对于数量少的一方,每一次都会匹配完,但也按照一般算法实施分配了
*/
void Match_dancer(Dancer* Male_head, Dancer* Female_head) {
Dancer* male_current = Male_head->next;
Dancer* female_current = Female_head->next;
}
/*
进行链表的测试
*/
void Print_list(Dancer* head) {
Dancer* current = head->next;
while (current)
{
printf("%s\t", current->name);
printf("%d\t", current->sex);
printf("%d", current->status);
current = current->next;
printf("\n");
}
}
int main() {
Dancer* head = Init_head();
Dancer* Male_head = Male_init_head();
Dancer* Female_head = Female_init_head();
Init_list(head, 2);
Print_list(head);
}