#define MAXVER 10 #define MAXEDG 13 typedef char VertexType; typedef struct EdgeNode { int v_id; struct EdgeNode* next_edge; }ENode; typedef struct VertexNode { VertexType val; ENode* first_edge; }VNode; typedef struct Graph { int vexnum; //顶点数 int edgenum; //边数 VNode vertexs[MAXVER]; }Graph; 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语言
时间: 2023-08-07 17:05:42 浏览: 137
根据初始条件,下面是这些函数的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXVER 10
#define MAXEDG 13
typedef char VertexType;
typedef struct EdgeNode {
int v_id;
struct EdgeNode* next_edge;
} ENode;
typedef struct VertexNode {
VertexType val;
ENode* first_edge;
} VNode;
typedef struct Graph {
int vexnum; // 顶点数
int edgenum; // 边数
VNode vertexs[MAXVER];
} Graph;
// 创建图
void creatGraph(Graph* G, VertexType ver[], VertexType edge[][2]) {
int i, j, k;
G->vexnum = sizeof(ver) / sizeof(VertexType);
G->edgenum = sizeof(edge) / sizeof(VertexType) / 2;
for (i = 0; i < G->vexnum; i++) {
G->vertexs[i].val = ver[i]; // 顶点数组赋值
G->vertexs[i].first_edge = NULL; // 初始化邻接表
}
for (k = 0; k < G->edgenum; k++) {
i = j = -1;
// 查找边的两个顶点在顶点数组中的下标
for (int l = 0; l < G->vexnum; l++) {
if (edge[k][0] == G->vertexs[l].val) {
i = l;
}
if (edge[k][1] == G->vertexs[l].val) {
j = l;
}
}
if (i != -1 && j != -1) {
// 创建边节点插入邻接表
ENode* p = (ENode*)malloc(sizeof(ENode));
p->v_id = j;
p->next_edge = G->vertexs[i].first_edge;
G->vertexs[i].first_edge = p;
p = (ENode*)malloc(sizeof(ENode));
p->v_id = i;
p->next_edge = G->vertexs[j].first_edge;
G->vertexs[j].first_edge = p;
}
}
}
// 深度优先遍历
void DFS(Graph* G, int k, int*_visit, char** res) {
int i;
res[*_visit] = &(G->vertexs[k].val); // 将遍历到的顶点存入 res 数组
*_visit += 1;
_visit[k] = 1; // 标记该顶点已被访问
ENode* p = G->vertexs[k].first_edge;
while (p) {
if (!_visit[p->v_id]) {
DFS(G, p->v_id, _visit, res); // 递归访问邻接顶点
}
p = p->next_edge;
}
}
// 广度优先遍历
void BFS(Graph* G, int*_visit, char res[]) {
int i, j, k;
int queue[MAXVER], front = 0, rear = 0;
for (i = 0; i < G->vexnum; i++) {
if (!_visit[i]) {
res[rear++] = G->vertexs[i].val; // 将未被访问的顶点加入队列
_visit[i] = 1; // 标记该顶点已被访问
}
while (front < rear) {
k = queue[front++]; // 出队一个顶点
ENode* p = G->vertexs[k].first_edge;
while (p) {
if (!_visit[p->v_id]) {
res[rear++] = G->vertexs[p->v_id].val; // 将邻接顶点加入队列
_visit[p->v_id] = 1; // 标记该顶点已被访问
}
p = p->next_edge;
}
}
}
}
// 销毁图
void DestroyGraph(Graph*G) {
G->vexnum = 0;
G->edgenum = 0;
for (int i = 0; i < G->vexnum; i++) {
ENode* p = G->vertexs[i].first_edge;
while (p) {
ENode* q = p;
p = p->next_edge;
free(q); // 释放邻接表节点
}
}
}
// 打印图
void Print(Graph*G) {
int i;
printf("顶点数:%d,边数:%d\n", G->vexnum, G->edgenum);
printf("顶点数组:");
for (i = 0; i < G->vexnum; i++) {
printf("%c ", G->vertexs[i].val);
}
printf("\n邻接表:\n");
for (i = 0; i < G->vexnum; i++) {
ENode* p = G->vertexs[i].first_edge;
printf("%c -> ", G->vertexs[i].val);
while (p) {
printf("%c ", G->vertexs[p->v_id].val);
p = p->next_edge;
}
printf("\n");
}
}
```
阅读全文