先依次输入有向图的顶点信息,再依次输入有向图的弧信息,建立图的邻接表存储结构并将其打印输出 ,然后在建立的有向图的邻接表存储结构基础上使用非递归算法对图进行深度优先搜索遍历并输出顶点序列,最后在建立的有向图的邻接表存储结构基础上对图进行广度优先搜索遍历并输出顶点序列。用C语言实现
时间: 2024-02-09 14:11:13 浏览: 74
好的,下面是修改过的C语言代码,实现了您所需的功能:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义邻接表结点
typedef struct Node {
int index; // 结点编号
struct Node *next; // 指向下一个邻接点
} Node;
// 定义邻接表
typedef struct Graph {
int vertex_num; // 结点数目
Node **adj_list; // 指向每个结点的邻接链表头指针的数组
} Graph;
// 初始化邻接表
Graph* init_graph(int vertex_num) {
Graph *graph = (Graph*) malloc(sizeof(Graph));
graph->vertex_num = vertex_num;
graph->adj_list = (Node**) malloc(vertex_num * sizeof(Node*));
for (int i = 0; i < vertex_num; i++) {
graph->adj_list[i] = NULL;
}
return graph;
}
// 添加边
void add_edge(Graph *graph, int start, int end) {
// 在起始结点的邻接链表头插入终止结点
Node *new_node = (Node*) malloc(sizeof(Node));
new_node->index = end;
new_node->next = graph->adj_list[start];
graph->adj_list[start] = new_node;
}
// 打印邻接表
void print_graph(Graph *graph) {
for (int i = 0; i < graph->vertex_num; i++) {
printf("Vertex %d: ", i);
Node *cur_node = graph->adj_list[i];
while (cur_node != NULL) {
printf("%d ", cur_node->index);
cur_node = cur_node->next;
}
printf("\n");
}
}
// 非递归深度优先搜索
void dfs_iterative(Graph *graph, int start) {
// 创建栈
int *stack = (int*) malloc(graph->vertex_num * sizeof(int));
int top = -1;
int visited[graph->vertex_num];
for (int i = 0; i < graph->vertex_num; i++) {
visited[i] = 0;
}
// 将起始结点入栈并标记已访问
stack[++top] = start;
visited[start] = 1;
// 遍历栈中所有结点的邻接点
while (top >= 0) {
int cur_vertex = stack[top--];
printf("%d ", cur_vertex);
Node *cur_node = graph->adj_list[cur_vertex];
while (cur_node != NULL) {
int index = cur_node->index;
if (!visited[index]) {
stack[++top] = index;
visited[index] = 1;
}
cur_node = cur_node->next;
}
}
// 释放栈
free(stack);
}
// 广度优先搜索
void bfs(Graph *graph, int start) {
// 创建队列
int *queue = (int*) malloc(graph->vertex_num * sizeof(int));
int front = 0, rear = -1;
int visited[graph->vertex_num];
for (int i = 0; i < graph->vertex_num; i++) {
visited[i] = 0;
}
// 将起始结点入队并标记已访问
queue[++rear] = start;
visited[start] = 1;
// 遍历队列中所有结点的邻接点
while (front <= rear) {
int cur_vertex = queue[front++];
printf("%d ", cur_vertex);
Node *cur_node = graph->adj_list[cur_vertex];
while (cur_node != NULL) {
int index = cur_node->index;
if (!visited[index]) {
queue[++rear] = index;
visited[index] = 1;
}
cur_node = cur_node->next;
}
}
// 释放队列
free(queue);
}
int main() {
// 输入有向图的顶点信息和边信息
int vertex_num, edge_num;
printf("Enter the number of vertices: ");
scanf("%d", &vertex_num);
printf("Enter the number of edges: ");
scanf("%d", &edge_num);
// 建立邻接表
Graph *graph = init_graph(vertex_num);
for (int i = 0; i < edge_num; i++) {
int start, end;
printf("Enter the start and end vertices of edge %d: ", i+1);
scanf("%d %d", &start, &end);
add_edge(graph, start, end);
}
// 输出邻接表
printf("Adjacency list:\n");
print_graph(graph);
// 非递归深度优先搜索
printf("Depth-first search (iterative): ");
dfs_iterative(graph, 0);
printf("\n");
// 广度优先搜索
printf("Breadth-first search: ");
bfs(graph, 0);
printf("\n");
// 释放邻接表
for (int i = 0; i < vertex_num; i++) {
Node *cur_node = graph->adj_list[i];
while (cur_node != NULL) {
Node *temp = cur_node;
cur_node = cur_node->next;
free(temp);
}
}
free(graph->adj_list);
free(graph);
return 0;
}
```
这段代码实现了以下功能:
1. 通过输入顶点信息和边信息建立有向图的邻接表存储结构并打印输出
2. 使用非递归算法对图进行深度优先搜索遍历并输出顶点序列
3. 对图进行广度优先搜索遍历并输出顶点序列
阅读全文