#include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 100 // 定义顶点的结构体 typedef struct { int data; } VertexType; // 定义边的结构体 typedef struct { int i, j; // 边的两个顶点下标 } EdgeType; // 定义邻接矩阵 typedef struct { int edges[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵 int n, e; // n 为顶点数,e 为边数 VertexType vertex[MAX_VERTEX_NUM]; // 顶点数组 } MGraph; // 初始化邻接矩阵 void CreateMGraph(MGraph *G) { int i, j, k, w; printf("请输入顶点数和边数:\n"); scanf("%d %d", &G->n, &G->e); printf("请输入顶点信息:\n"); for (i = 0; i < G->n; i++) { scanf("%d", &G->vertex[i].data); } for (i = 0; i < G->n; i++) { for (j = 0; j < G->n; j++) { G->edges[i][j] = 0; } } printf("请输入边信息:\n"); for (k = 0; k < G->e; k++) { printf("请输入第 %d 条边的下标 i, j 和权重 w:", k+1); scanf("%d %d %d", &i, &j, &w); G->edges[i][j] = w; G->edges[j][i] = w; } } // 深度优先搜索 void DFS(MGraph *G, int v, int *visited) { int i; visited[v] = 1; printf("%d ", G->vertex[v].data); for (i = 0; i < G->n; i++) { if (G->edges[v][i] && !visited[i]) { DFS(G, i, visited); } } } int main() { MGraph G; int visited[MAX_VERTEX_NUM] = {0}; CreateMGraph(&G); printf("深度优先搜索结果为:\n"); DFS(&G, 0, visited); printf("\n"); return 0; }
时间: 2024-03-30 14:40:41 浏览: 172
这是一段使用邻接矩阵表示图,并进行深度优先搜索的 C 语言程序。程序先定义了顶点的构体、边的构体和邻接矩的结构体,后实现了初始化邻矩阵的函数 CreateGraph() 和深度优先搜索的函数 DFS()。
在主函数中,程序首先创建了一个邻接矩阵图 G,然后定义了一个 visited 数组表示是否访问过该顶点,并将其初始化为 0。接下来程序调用 DFS() 函数对图进行深度优先搜索,并输出搜索结果。
注意,这段程序的正确性前提是用户输入的图是连通图,否则可能无法搜索到所有的顶点。
相关问题
优化这段代码#include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<conio.h> #define OK 1 #define error 0 #define MVNum 100 #define MAXSIZE 10 typedef int OtherInfo,QElemtype; typedef char VerTexType; //结构体定义 typedef struct ArcNode{ int adjvex;
首先,该段代码中包含了一些不必要的头文件,例如:conio.h,malloc.h等。建议只保留stdio.h和stdlib.h。
其次,可以将#define OK 1和#define error 0替换为枚举类型,使代码更加清晰易读。
最后,在结构体定义中,可以将OtherInfo和QElemtype合并为一个类型,避免定义过多类型。同时,也可以将结构体中的变量类型进行优化,例如将int类型的adjvex改为short或者char类型。
重构后的代码如下所示:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 100
#define MAX_ARC_NUM 10
typedef enum {
ERROR = 0,
OK = 1
} Status;
typedef char VertexType;
typedef int ArcType;
typedef struct ArcNode {
int adjvex;
struct ArcNode* nextarc;
ArcType* info;
} ArcNode;
typedef struct VNode {
VertexType data;
ArcNode* firstarc;
} VNode, AdjList[MAX_VERTEX_NUM];
typedef struct {
AdjList vertices;
int vexnum, arcnum;
} ALGraph;
```
希望可以帮到你。
1. (程序题) 创建无向图的邻接链表。已知无向图的数据结构声明语句如下: #include<stdio.h> #include<stdlib.h> #define MAX 20 typedef c
在C语言中,创建无向图的邻接链表通常需要定义一个结构体表示顶点(Node),以及一个结构体表示链表节点(Adjacency List Node)。这里给出一个简单的示例:
首先,定义顶点结构体 `Vertex`,包含一个整数值(表示顶点编号)和一个指向链表头结点的指针:
```c
typedef struct Vertex {
int vertex_id;
struct AdjListNode* adjList;
} Vertex;
```
接着,定义链表节点结构体 `AdjListNode`,它存储另一个顶点的引用,并有一个指向下一个节点的指针:
```c
typedef struct AdjListNode {
Vertex* neighbor;
struct AdjListNode* next;
} AdjListNode;
```
然后,你可以定义一个全局数组或动态分配内存来存放所有顶点,同时初始化每个顶点的邻接列表头指针为NULL:
```c
Vertex vertices[MAX];
for (int i = 0; i < MAX; i++) {
vertices[i].vertex_id = i;
vertices[i].adjList = NULL;
}
```
最后,为了添加边(即连接两个顶点),你需要遍历邻接链表并插入新的节点。例如,如果要将顶点A和顶点B相连:
```c
void addEdge(int src, int dest) {
AdjListNode* newNode = (AdjListNode*)malloc(sizeof(AdjListNode));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
return;
}
newNode->neighbor = &vertices[dest]; // 将邻居指向目标顶点
newNode->next = vertices[src].adjList; // 将新节点插入到源顶点的链表头
vertices[src].adjList = newNode; // 更新源顶点的邻接链表
}
```
记得处理可能出现的边界情况和释放动态内存。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.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)