家谱管理系统数据结构c语音
时间: 2023-12-04 09:40:57 浏览: 171
家谱管理系统C语言.zip
根据提供的引用内容,家谱管理系统的数据结构可以使用二叉树来表示。每个成员包含姓名、性别、妻子、出生日期、死亡日期等信息。假定每个成员最多有两个孩子,女儿的孩子不进家谱。在C语言中,可以使用结构体来表示每个成员的信息,使用指针来表示二叉树的节点。以下是一个简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义家族成员结构体
typedef struct member {
char name[20]; // 姓名
char gender; // 性别
char wife[20]; // 妻子姓名
char birth[20]; // 出生日期
char death[20]; // 死亡日期
struct member *left; // 左孩子指针
struct member *right; // 右孩子指针
} Member;
// 创建新成员
Member *create_member(char *name, char gender, char *wife, char *birth, char *death) {
Member *member = (Member *)malloc(sizeof(Member));
strcpy(member->name, name);
member->gender = gender;
strcpy(member->wife, wife);
strcpy(member->birth, birth);
strcpy(member->death, death);
member->left = NULL;
member->right = NULL;
return member;
}
// 插入新成员
void insert_member(Member **root, Member *member) {
if (*root == NULL) {
*root = member;
} else {
if (strcmp(member->name, (*root)->name) < 0) {
insert_member(&((*root)->left), member);
} else {
insert_member(&((*root)->right), member);
}
}
}
// 按姓名查找成员
Member *find_member(Member *root, char *name) {
if (root == NULL) {
return NULL;
} else {
if (strcmp(name, root->name) == 0) {
return root;
} else if (strcmp(name, root->name) < 0) {
return find_member(root->left, name);
} else {
return find_member(root->right, name);
}
}
}
// 打印家族成员信息
void print_member(Member *member) {
printf("姓名:%s,性别:%c,妻子:%s,出生日期:%s,死亡日期:%s\n", member->name, member->gender, member->wife, member->birth, member->death);
}
// 中序遍历家族成员
void inorder_traversal(Member *root) {
if (root != NULL) {
inorder_traversal(root->left);
print_member(root);
inorder_traversal(root->right);
}
}
int main() {
Member *root = NULL;
Member *member1 = create_member("张三", 'M', "李四", "1980-01-01", "");
Member *member2 = create_member("李四", 'F', "", "1985-01-01", "");
Member *member3 = create_member("王五", 'M', "赵六", "1990-01-01", "");
insert_member(&root, member1);
insert_member(&root, member2);
insert_member(&root, member3);
inorder_traversal(root);
Member *find = find_member(root, "张三");
if (find != NULL) {
print_member(find);
}
return 0;
}
```
阅读全文