用c语言设计一个基于DOS菜单的应用程序。内容如下: 无向图的基本操作及应用 ① 创建无向图的邻接矩阵并打印 ② 创建无向图的邻接表并打印 ③ 无向图的深度优先遍历 ④ 无向图的广度优先遍历
时间: 2024-02-09 14:09:50 浏览: 161
数据结构-c语言-带main函数-图7.3-图的遍历-广度优先搜索-队列-邻接表-无向图。
5星 · 资源好评率100%
好的,我可以为您提供一些关于用 C 语言设计基于 DOS 菜单的应用程序的建议。首先,您需要使用 C 语言编写代码来实现所需的功能,包括创建无向图、打印邻接矩阵或邻接表、深度优先遍历和广度优先遍历。接下来,您需要使用 DOS 命令行界面来创建交互式菜单,其中每个选项都对应着一个功能。您可以使用 switch 语句来实现菜单选项的选择,以及调用相应的函数来执行所需的操作。以下是一个简单的示例代码,供您参考:
```
#include <stdio.h>
#include <stdlib.h>
/* 定义无向图结构体 */
typedef struct {
int** adj_matrix; // 邻接矩阵
int num_vertices; // 顶点数
} Graph;
/* 创建无向图邻接矩阵 */
Graph* create_adj_matrix(int num_vertices) {
Graph* graph = (Graph*) malloc(sizeof(Graph));
graph->num_vertices = num_vertices;
graph->adj_matrix = (int**) malloc(num_vertices * sizeof(int*));
for (int i = 0; i < num_vertices; i++) {
graph->adj_matrix[i] = (int*) malloc(num_vertices * sizeof(int));
for (int j = 0; j < num_vertices; j++) {
graph->adj_matrix[i][j] = 0;
}
}
return graph;
}
/* 打印邻接矩阵 */
void print_adj_matrix(Graph* graph) {
printf("Adjacency Matrix:\n");
for (int i = 0; i < graph->num_vertices; i++) {
for (int j = 0; j < graph->num_vertices; j++) {
printf("%d ", graph->adj_matrix[i][j]);
}
printf("\n");
}
}
/* 创建无向图邻接表 */
Graph* create_adj_list(int num_vertices) {
Graph* graph = (Graph*) malloc(sizeof(Graph));
graph->num_vertices = num_vertices;
graph->adj_matrix = (int**) malloc(num_vertices * sizeof(int*));
for (int i = 0; i < num_vertices; i++) {
graph->adj_matrix[i] = NULL;
}
return graph;
}
/* 打印邻接表 */
void print_adj_list(Graph* graph) {
printf("Adjacency List:\n");
for (int i = 0; i < graph->num_vertices; i++) {
printf("%d: ", i);
for (int j = 0; j < graph->num_vertices; j++) {
if (graph->adj_matrix[i][j] == 1) {
printf("%d ", j);
}
}
printf("\n");
}
}
/* 深度优先遍历 */
void dfs(Graph* graph, int vertex, int* visited) {
visited[vertex] = 1;
printf("%d ", vertex);
for (int i = 0; i < graph->num_vertices; i++) {
if (graph->adj_matrix[vertex][i] == 1 && visited[i] == 0) {
dfs(graph, i, visited);
}
}
}
/* 广度优先遍历 */
void bfs(Graph* graph, int vertex, int* visited) {
int queue[graph->num_vertices];
int front = 0;
int rear = -1;
visited[vertex] = 1;
queue[++rear] = vertex;
while (front <= rear) {
int v = queue[front++];
printf("%d ", v);
for (int i = 0; i < graph->num_vertices; i++) {
if (graph->adj_matrix[v][i] == 1 && visited[i] == 0) {
visited[i] = 1;
queue[++rear] = i;
}
}
}
}
/* 主函数 */
int main() {
int choice;
int num_vertices;
Graph* graph;
int* visited;
/* 显示菜单选项 */
do {
printf("\n");
printf("1. Create adjacency matrix and print\n");
printf("2. Create adjacency list and print\n");
printf("3. Depth first search\n");
printf("4. Breadth first search\n");
printf("5. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
/* 创建邻接矩阵 */
printf("Enter number of vertices: ");
scanf("%d", &num_vertices);
graph = create_adj_matrix(num_vertices);
visited = (int*) malloc(num_vertices * sizeof(int));
for (int i = 0; i < num_vertices; i++) {
visited[i] = 0;
}
/* 打印邻接矩阵 */
print_adj_matrix(graph);
break;
case 2:
/* 创建邻接表 */
printf("Enter number of vertices: ");
scanf("%d", &num_vertices);
graph = create_adj_list(num_vertices);
visited = (int*) malloc(num_vertices * sizeof(int));
for (int i = 0; i < num_vertices; i++) {
visited[i] = 0;
}
/* 打印邻接表 */
print_adj_list(graph);
break;
case 3:
/* 深度优先遍历 */
printf("Enter starting vertex: ");
scanf("%d", &num_vertices);
dfs(graph, num_vertices, visited);
break;
case 4:
/* 广度优先遍历 */
printf("Enter starting vertex: ");
scanf("%d", &num_vertices);
bfs(graph, num_vertices, visited);
break;
case 5:
/* 退出程序 */
exit(0);
default:
printf("Invalid choice!\n");
}
} while (choice != 5);
return 0;
}
```
请注意,以上代码仅提供了一种可能的实现方式,您可以根据自己的需求进行修改和优化。同时,您还需要在 DOS 命令行环境中编译和运行该程序。
阅读全文