用c语言编程实现图的邻接表或邻接矩阵表示,并创建图,再对邻接表或邻接矩阵表示的图实现深度优先遍历。请给出完整的代码
时间: 2023-11-25 14:16:12 浏览: 107
好的,我会尽力回答您的问题。以下是用 C 语言实现图的邻接表和邻接矩阵表示、以及深度优先遍历的完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义邻接表结构体
typedef struct node {
int vertex;
struct node* next;
} Node;
// 定义图结构体
typedef struct graph {
int num_vertices;
Node** adj_list;
int* visited;
} Graph;
// 创建邻接表节点
Node* create_node(int v) {
Node* new_node = (Node*) malloc(sizeof(Node));
new_node->vertex = v;
new_node->next = NULL;
return new_node;
}
// 创建图
Graph* create_graph(int num_vertices) {
Graph* graph = (Graph*) malloc(sizeof(Graph));
graph->num_vertices = num_vertices;
graph->adj_list = (Node**) malloc(num_vertices * sizeof(Node*));
graph->visited = (int*) malloc(num_vertices * sizeof(int));
int i;
for (i = 0; i < num_vertices; i++) {
graph->adj_list[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
// 添加边到邻接表
void add_edge(Graph* graph, int src, int dest) {
Node* new_node = create_node(dest);
new_node->next = graph->adj_list[src];
graph->adj_list[src] = new_node;
new_node = create_node(src);
new_node->next = graph->adj_list[dest];
graph->adj_list[dest] = new_node;
}
// 创建邻接矩阵
int** create_matrix(int num_vertices) {
int** matrix = (int**) malloc(num_vertices * sizeof(int*));
int i, j;
for (i = 0; i < num_vertices; i++) {
matrix[i] = (int*) malloc(num_vertices * sizeof(int));
for (j = 0; j < num_vertices; j++) {
matrix[i][j] = 0;
}
}
return matrix;
}
// 添加边到邻接矩阵
void add_edge_matrix(int** matrix, int src, int dest) {
matrix[src][dest] = 1;
matrix[dest][src] = 1;
}
// 深度优先遍历
void dfs(Graph* graph, int vertex) {
graph->visited[vertex] = 1;
printf("%d ", vertex);
Node* adj_list = graph->adj_list[vertex];
while (adj_list != NULL) {
int adj_vertex = adj_list->vertex;
if (graph->visited[adj_vertex] == 0) {
dfs(graph, adj_vertex);
}
adj_list = adj_list->next;
}
}
int main() {
// 创建邻接表表示的图
Graph* graph_list = create_graph(5);
add_edge(graph_list, 0, 1);
add_edge(graph_list, 0, 2);
add_edge(graph_list, 1, 2);
add_edge(graph_list, 2, 3);
add_edge(graph_list, 3, 4);
printf("深度优先遍历邻接表表示的图:\n");
dfs(graph_list, 0);
printf("\n");
// 创建邻接矩阵表示的图
int** matrix = create_matrix(5);
add_edge_matrix(matrix, 0, 1);
add_edge_matrix(matrix, 0, 2);
add_edge_matrix(matrix, 1, 2);
add_edge_matrix(matrix, 2, 3);
add_edge_matrix(matrix, 3, 4);
printf("深度优先遍历邻接矩阵表示的图:\n");
int i, j;
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (matrix[i][j] == 1 && graph_list->visited[i] == 0) {
dfs(graph_list, i);
}
}
}
printf("\n");
return 0;
}
```
在上面的代码中,我们首先定义了邻接表和图的结构体,然后分别实现了创建邻接表和图、添加边到邻接表和邻接矩阵、以及深度优先遍历的函数。最后在 `main` 函数中,我们创建了一个邻接表表示的图和一个邻接矩阵表示的图,并对它们进行了深度优先遍历。
希望这份代码能够帮到您!
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.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://img-home.csdnimg.cn/images/20241231044901.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://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)