数据结构课程设计家谱c语言
时间: 2023-10-26 11:03:35 浏览: 221
好的,那么我可以为您提供一些关于家谱设计的C语言代码思路。
首先,我们需要确定家谱中每个成员的基本信息,这些信息可以是姓名、性别、出生日期、父亲、母亲等。我们可以使用结构体来表示每个成员的信息:
```
struct member {
char name[20];
char gender;
char birthdate[10];
struct member *father;
struct member *mother;
};
```
上述结构体中,name表示成员的姓名,gender表示成员的性别('M'或'F'),birthdate表示成员的出生日期,father和mother分别表示成员的父亲和母亲。
接下来,我们可以使用链表来表示家族中每个成员之间的关系。每个节点表示一个成员,节点中的数据域表示成员的信息,指针域表示成员的父亲和母亲。
```
struct member_node {
struct member data;
struct member_node *father;
struct member_node *mother;
};
```
在创建家谱时,我们可以先创建一个根节点,表示家族的起源。然后,通过输入每个成员的信息,依次添加到家谱中。当添加一个新成员时,需要指定其父亲和母亲,可以通过遍历家谱来查找对应的节点。
最后,我们可以提供一些基本的操作,如添加新成员、查找成员、删除成员等。
下面是一份简单的家谱管理代码,仅供参考:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct member {
char name[20];
char gender;
char birthdate[10];
struct member *father;
struct member *mother;
};
struct member_node {
struct member data;
struct member_node *father;
struct member_node *mother;
};
struct member_node *root = NULL;
struct member_node *create_node(struct member data) {
struct member_node *node = (struct member_node*)malloc(sizeof(struct member_node));
node->data = data;
node->father = NULL;
node->mother = NULL;
return node;
}
struct member_node *find_member(char *name) {
struct member_node *node = root;
while (node != NULL) {
if (strcmp(node->data.name, name) == 0) {
return node;
}
node = node->father;
}
node = root;
while (node != NULL) {
if (strcmp(node->data.name, name) == 0) {
return node;
}
node = node->mother;
}
return NULL;
}
void add_member(struct member data, char *father_name, char *mother_name) {
struct member_node *father = NULL, *mother = NULL;
if (father_name != NULL) {
father = find_member(father_name);
if (father == NULL) {
printf("Error: Father does not exist.\n");
return;
}
}
if (mother_name != NULL) {
mother = find_member(mother_name);
if (mother == NULL) {
printf("Error: Mother does not exist.\n");
return;
}
}
struct member_node *node = create_node(data);
node->data.father = father == NULL ? NULL : &father->data;
node->data.mother = mother == NULL ? NULL : &mother->data;
if (root == NULL) {
root = node;
} else {
if (father != NULL) {
node->father = father;
} else {
node->mother = mother;
}
}
}
void delete_member(char *name) {
struct member_node *node = find_member(name);
if (node == NULL) {
printf("Error: Member does not exist.\n");
return;
}
if (node == root) {
root = NULL;
free(node);
return;
}
if (node->father != NULL) {
if (node->father->father == node) {
node->father->father = NULL;
} else {
node->father->mother = NULL;
}
} else {
if (node->mother->father == node) {
node->mother->father = NULL;
} else {
node->mother->mother = NULL;
}
}
free(node);
}
void print_member(struct member data) {
printf("Name: %s\n", data.name);
printf("Gender: %c\n", data.gender);
printf("Birthdate: %s\n", data.birthdate);
if (data.father != NULL) {
printf("Father: %s\n", data.father->name);
}
if (data.mother != NULL) {
printf("Mother: %s\n", data.mother->name);
}
}
void print_family() {
struct member_node *node = root;
while (node != NULL) {
print_member(node->data);
printf("\n");
node = node->father;
}
node = root;
while (node != NULL) {
print_member(node->data);
printf("\n");
node = node->mother;
}
}
int main() {
struct member data;
strcpy(data.name, "Adam");
data.gender = 'M';
strcpy(data.birthdate, "01/01/0001");
add_member(data, NULL, NULL);
strcpy(data.name, "Eve");
data.gender = 'F';
strcpy(data.birthdate, "01/01/0001");
add_member(data, NULL, NULL);
strcpy(data.name, "Cain");
data.gender = 'M';
strcpy(data.birthdate, "01/01/0020");
add_member(data, "Adam", "Eve");
strcpy(data.name, "Abel");
data.gender = 'M';
strcpy(data.birthdate, "01/01/0022");
add_member(data, "Adam", "Eve");
printf("Family Tree:\n");
print_family();
delete_member("Abel");
printf("Family Tree after Abel's death:\n");
print_family();
return 0;
}
```
阅读全文