用c完善下面代码#include<stdio.h> #include<stdlib.h> typedefintVertex; #defineMaxVertexNum10 typedefstructGNode*PtrToGNode; structGNode{ intNv;/*顶点数*/ intNe;/*边数*/ intG[MaxVertexNum][MaxVertexNum]; }; typedefPtrToGNodeMGraph; intVisited[MaxVertexNum];/*记录某个顶点是否被访问过*/ voidDFS(MGraphGraph,VertexV) { VertexW; printf("%d",V); Visited[V]=1; 11 数据结构与算法基础实验指导 /*待补充代码1*/ } intmain() { /*待补充代码2*/ }实现邻接矩阵存储图的深度优先遍历(访问函数设定为输出当前顶点值)
时间: 2023-08-08 11:07:36 浏览: 129
<stdlibh>通用实用工具-达内内部C语言PPT
补充代码1:
```
for(W = 0; W < Graph->Nv; W++) {
if(Graph->G[V][W] && !Visited[W]) {
DFS(Graph, W);
}
}
```
补充代码2:
```
int main() {
int i, j;
MGraph Graph = (MGraph)malloc(sizeof(struct GNode));
scanf("%d %d", &Graph->Nv, &Graph->Ne);
for(i = 0; i < Graph->Nv; i++) {
for(j = 0; j < Graph->Nv; j++) {
Graph->G[i][j] = 0;
}
}
for(i = 0; i < Graph->Ne; i++) {
int v1, v2;
scanf("%d %d", &v1, &v2);
Graph->G[v1][v2] = 1;
Graph->G[v2][v1] = 1;
}
for(i = 0; i < Graph->Nv; i++) {
Visited[i] = 0;
}
for(i = 0; i < Graph->Nv; i++) {
if(!Visited[i]) {
DFS(Graph, i);
}
}
return 0;
}
```
阅读全文