帮我转换成c语言吧
时间: 2023-07-13 17:20:58 浏览: 60
好的,这是一段 C++ 代码,是一个基于邻接矩阵的图的广度优先遍历算法。
以下是转换后的 C 代码:
```c
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define MVNum 50
typedef struct {
bool visited[MVNum]; // 辅助数组
int arcs[MVNum][MVNum]; // 邻接矩阵
int n;
} AMGraph; // 邻接矩阵的结构体
void BFS_traverse(int v, AMGraph *G) {
printf("%d ", v);
G->visited[v] = true;
int front;
int *queue = malloc(sizeof(int) * G->n);
int front_index = 0;
int rear_index = 0;
queue[rear_index++] = v;
while (front_index < rear_index) {
front = queue[front_index++];
for (int i = 0; i < G->n; i++) {
if (G->arcs[front][i] != 0 && !G->visited[i]) {
printf("%d ", i);
G->visited[i] = true;
queue[rear_index++] = i;
}
}
}
free(queue);
}
int main() {
AMGraph G;
scanf("%d", &G.n);
for (int i = 0; i < G.n; i++) {
G.visited[i] = false;
for (int j = 0; j < G.n; j++) {
scanf("%d", &G.arcs[i][j]);
}
}
BFS_traverse(0, &G);
return 0;
}
```
请注意,由于 C 语言中没有自带的队列数据结构,因此在代码中我们手动实现了一个队列,使用数组来模拟队列。另外,由于 C 语言中没有 bool 类型,我们使用了 stdbool.h 头文件中的 bool 类型。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)