void creatGraph(Graph* G, VertexType ver[], VertexType edge[][2]); void DFS(Graph* G, int k, int*_visit, char** res); void BFS(Graph* G, int*_visit, char res[]); void DestroyGraph(Graph*G); void Print(Graph*G);用c语言补全这些函数代码

时间: 2024-02-12 12:05:18 浏览: 27
下面是这些函数的代码实现: ```c #define MAX_VERTEX_NUM 100 // 最大顶点数 typedef char VertexType; // 顶点类型 typedef struct { VertexType vexs[MAX_VERTEX_NUM]; // 顶点数组 int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵 int vexnum, arcnum; // 顶点数,边数 } Graph; // 创建图 void creatGraph(Graph* G, VertexType ver[], VertexType edge[][2]) { int i, j, k; G->vexnum = sizeof(ver) / sizeof(VertexType); G->arcnum = sizeof(edge) / sizeof(VertexType) / 2; for (i = 0; i < G->vexnum; i++) { G->vexs[i] = ver[i]; // 顶点数组赋值 } for (i = 0; i < G->vexnum; i++) { for (j = 0; j < G->vexnum; j++) { G->arcs[i][j] = 0; // 初始化邻接矩阵 } } for (k = 0; k < G->arcnum; k++) { i = j = -1; // 查找边的两个顶点在顶点数组中的下标 for (int l = 0; l < G->vexnum; l++) { if (edge[k][0] == G->vexs[l]) { i = l; } if (edge[k][1] == G->vexs[l]) { j = l; } } if (i != -1 && j != -1) { G->arcs[i][j] = G->arcs[j][i] = 1; // 无向图,两个方向都要标记 } } } // 深度优先遍历 void DFS(Graph* G, int k, int*_visit, char** res) { int i; res[*_visit] = &(G->vexs[k]); // 将遍历到的顶点存入 res 数组 *_visit += 1; _visit[k] = 1; // 标记该顶点已被访问 for (i = 0; i < G->vexnum; i++) { if (G->arcs[k][i] && !_visit[i]) { DFS(G, i, _visit, res); // 递归访问邻接顶点 } } } // 广度优先遍历 void BFS(Graph* G, int*_visit, char res[]) { int i, j, k; int queue[MAX_VERTEX_NUM], front = 0, rear = 0; for (i = 0; i < G->vexnum; i++) { if (!_visit[i]) { res[rear++] = G->vexs[i]; // 将未被访问的顶点加入队列 _visit[i] = 1; // 标记该顶点已被访问 } while (front < rear) { k = queue[front++]; // 出队一个顶点 for (j = 0; j < G->vexnum; j++) { if (G->arcs[k][j] && !_visit[j]) { res[rear++] = G->vexs[j]; // 将邻接顶点加入队列 _visit[j] = 1; // 标记该顶点已被访问 } } } } } // 销毁图 void DestroyGraph(Graph*G) { G->vexnum = 0; G->arcnum = 0; } // 打印图 void Print(Graph*G) { int i, j; printf("顶点数:%d,边数:%d\n", G->vexnum, G->arcnum); printf("顶点数组:"); for (i = 0; i < G->vexnum; i++) { printf("%c ", G->vexs[i]); } printf("\n邻接矩阵:\n"); for (i = 0; i < G->vexnum; i++) { for (j = 0; j < G->vexnum; j++) { printf("%d ", G->arcs[i][j]); } printf("\n"); } } ```

相关推荐

#pragma once #include"SequenceList.h" typedef struct { SequenceList Vertices; int edge[MaxVertices][MaxVertices]; int numOfEdges; }MatrixGraph; void Initiate(MatrixGraph* G, int n) { int i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { if (i = j) G->edge[i][j] = 0; else G->edge[i][j] = MaxWeight; } G->numOfEdges = 0; ListInitialize(&G->Vertices); } void InsertVertex(MatrixGraph* G, ElemType vertex) { ListInsert(&G->Vertices, G->Vertices.size, vertex); } void InsertEdge(MatrixGraph* G, int v1, int v2, int weight) { if (v1 < 0 || v1 >= G->Vertices.size || v2 < 0 || v2 >= G->Vertices.size) { printf("参数v1或v2越界出错\n"); exit(1); } G->edge[v1][v2] = weight; G->numOfEdges++; } void DeleteEdge(MatrixGraph* G, int v1, int v2) { if (v1 < 0 || v1 >= G->Vertices.size || v2 < 0 || v2 >= G->Vertices.size || v1 == v2) { printf("参数v1或v2越界出错\n"); exit(1); } G->edge[v1][v2] = MaxWeight; G->numOfEdges--; } void DeleteVertex(MatrixGraph* G, int v) { int n = ListLength(G->Vertices), i, j; ElemType x; for (i = 0; i < n; i++) for (j = 0; j < n; j++) if ((i == v || j == v) && G->edge[i][j] > 0 && G->edge[i][j] < MaxWeight) G->numOfEdges--; for (i = v; i < n; i++) for (j = 0; j < n; j++) G->edge[i][j] = G->edge[i + 1][j]; for (i = 0; i < n; i++) for (j = v; j < n; j++) G->edge[i][j] = G->edge[i][j + 1]; ListDelete(&G->Vertices, v, &x); } typedef struct { int row; int col; int weight; }RowColWeight; void CreatGraph(MatrixGraph* G, ElemType V[], int n, RowColWeight E[], int e) { int i, k; Initiate(G, n); for (i = 0; i < n; i++) InsertVertex(G, V[i]); for (k = 0; k < e; k++) InsertEdge(G, E[k].row, E[k].col, E[k].weight); } #include<stdio.h> #include<string.h> #define MaxWeight 10000 #define MaxVertices 6 #define MaxSize 100 typedef int ElemType; #include"MGraph.h" void main(void) { MatrixGraph g1; ElemType a[] = { '1','2','3','4','5','6' }; RowColWeight rcw[] = { {0,2,5},{1,0,3},{1,4,8},{2,1,15},{2,5,7},{4,3,4},{5,3,10},{5,4,18} }; int n = 6, e = 6; int i, j; CreatGraph(&g1, a, n, rcw, e); printf("顶点集合为"); for (i = 0; i < g1.Vertices.size; i++) { printf("%c", g1.Vertices.list[i]); } printf("\n"); printf("权值集合为:\n"); for (i = 0; i < g1.Vertices.size; i++) { for (j = 0; j < g1.Vertices.size; j++) printf("%5d", g1.edge[i][j]); printf("\n"); } }为何会乱码,无法正常显示,以及给出解决后的代码

最新推荐

recommend-type

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a
recommend-type

matlab建立计算力学课程的笔记和文件.zip

matlab建立计算力学课程的笔记和文件.zip
recommend-type

FT-Prog-v3.12.38.643-FTD USB 工作模式设定及eprom读写

FT_Prog_v3.12.38.643--FTD USB 工作模式设定及eprom读写
recommend-type

matlab基于RRT和人工势场法混合算法的路径规划.zip

matlab基于RRT和人工势场法混合算法的路径规划.zip
recommend-type

matlab基于matlab的两步定位软件定义接收机的开源GNSS直接位置估计插件模块.zip

matlab基于matlab的两步定位软件定义接收机的开源GNSS直接位置估计插件模块.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。