用c语言代码建立三国人物关系图谱
时间: 2023-10-11 22:08:27 浏览: 177
好的,下面是建立三国人物关系图谱的代码示例,采用邻接表的形式存储图:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VERTICES 1000
#define MAX_NAME_LEN 20
typedef struct Vertex Vertex;
typedef struct Edge Edge;
struct Vertex {
char name[MAX_NAME_LEN];
char country[MAX_NAME_LEN];
Edge* first_edge;
};
struct Edge {
Vertex* dest;
char rel[MAX_NAME_LEN];
Edge* next_edge;
};
Vertex* vertices[MAX_VERTICES];
int num_vertices = 0;
Vertex* create_vertex(char* name, char* country) {
Vertex* v = (Vertex*) malloc(sizeof(Vertex));
strcpy(v->name, name);
strcpy(v->country, country);
v->first_edge = NULL;
return v;
}
void add_vertex(char* name, char* country) {
Vertex* v = create_vertex(name, country);
vertices[num_vertices++] = v;
}
void add_edge(Vertex* src, Vertex* dest, char* rel) {
Edge* e = (Edge*) malloc(sizeof(Edge));
e->dest = dest;
strcpy(e->rel, rel);
e->next_edge = src->first_edge;
src->first_edge = e;
}
void build_graph() {
// 添加顶点
add_vertex("刘备", "蜀国");
add_vertex("关羽", "蜀国");
add_vertex("张飞", "蜀国");
add_vertex("诸葛亮", "蜀国");
add_vertex("赵云", "蜀国");
add_vertex("马超", "蜀国");
add_vertex("黄忠", "蜀国");
add_vertex("魏延", "蜀国");
add_vertex("庞统", "蜀国");
add_vertex("姜维", "蜀国");
add_vertex("孙权", "吴国");
add_vertex("周瑜", "吴国");
add_vertex("诸葛瑾", "吴国");
add_vertex("陆逊", "吴国");
add_vertex("甘宁", "吴国");
add_vertex("吕蒙", "吴国");
add_vertex("太史慈", "吴国");
add_vertex("孙策", "吴国");
add_vertex("曹操", "魏国");
add_vertex("张辽", "魏国");
add_vertex("许褚", "魏国");
add_vertex("郭嘉", "魏国");
add_vertex("荀彧", "魏国");
add_vertex("曹丕", "魏国");
add_vertex("司马懿", "魏国");
add_vertex("张郃", "魏国");
add_vertex("夏侯惇", "魏国");
add_vertex("典韦", "魏国");
// 添加边
add_edge(vertices[0], vertices[1], "义弟");
add_edge(vertices[0], vertices[2], "义弟");
add_edge(vertices[0], vertices[3], "丞相");
add_edge(vertices[0], vertices[4], "亲信");
add_edge(vertices[0], vertices[5], "部将");
add_edge(vertices[0], vertices[6], "部将");
add_edge(vertices[0], vertices[7], "部将");
add_edge(vertices[0], vertices[8], "谋士");
add_edge(vertices[0], vertices[9], "部将");
add_edge(vertices[10], vertices[11], "主公");
add_edge(vertices[10], vertices[12], "部将");
add_edge(vertices[10], vertices[13], "谋士");
add_edge(vertices[10], vertices[14], "部将");
add_edge(vertices[10], vertices[15], "部将");
add_edge(vertices[10], vertices[16], "部将");
add_edge(vertices[10], vertices[17], "主公");
add_edge(vertices[18], vertices[19], "部将");
add_edge(vertices[18], vertices[20], "部将");
add_edge(vertices[18], vertices[21], "部将");
add_edge(vertices[18], vertices[22], "谋士");
add_edge(vertices[18], vertices[23], "谋士");
add_edge(vertices[18], vertices[24], "皇帝");
add_edge(vertices[18], vertices[25], "皇帝");
add_edge(vertices[18], vertices[26], "谋士");
add_edge(vertices[18], vertices[27], "部将");
add_edge(vertices[18], vertices[28], "部将");
}
void print_graph() {
FILE* fp = fopen("TRKGraph.txt", "w");
for (int i = 0; i < num_vertices; i++) {
Vertex* v = vertices[i];
fprintf(fp, "%s(%s)-->", v->name, v->country);
Edge* e = v->first_edge;
while (e != NULL) {
fprintf(fp, "%s(%s)-->", e->dest->name, e->dest->country);
e = e->next_edge;
}
fprintf(fp, "NULL\n");
}
fclose(fp);
}
int main() {
build_graph();
print_graph();
return 0;
}
```
这段代码会生成文件 "TRKGraph.txt",其中保存了邻接表的信息。你可以根据需求进行修改和扩展。
阅读全文