用c语言设计一个基于DOS菜单的应用程序。内容如下: 1. 无向图的基本操作及应用 ① 创建无向图的邻接矩阵并打印 ② 创建无向图的邻接表并打印③ 无向图的深度优先遍历 ④ 无向图的广度优先遍历
时间: 2024-02-03 22:13:44 浏览: 112
用C语言做一个简单的窗口程序
4星 · 用户满意度95%
以下是一个简单的基于DOS菜单的应用程序,实现了无向图的基本操作及应用:
```c
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
// 无向图邻接矩阵
#define MAX_VERTEX_NUM 20
typedef struct {
char vexs[MAX_VERTEX_NUM]; // 顶点集合
int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵
int vexnum, arcnum; // 顶点数、边数
} MGraph;
// 无向图邻接表
#define MAX_ARC_NUM 50
typedef struct ArcNode {
int adjvex; // 邻接点下标
struct ArcNode *next; // 指向下一个邻接点的指针
} ArcNode;
typedef struct VNode {
char data; // 顶点信息
ArcNode *first; // 指向第一个邻接点的指针
} VNode, AdjList[MAX_VERTEX_NUM];
typedef struct {
AdjList vertices; // 邻接表
int vexnum, arcnum; // 顶点数、边数
} ALGraph;
// 创建无向图的邻接矩阵并打印
void createMGraph(MGraph *G) {
int i, j, k, w;
printf("请输入顶点数和边数:");
scanf("%d%d", &G->vexnum, &G->arcnum);
printf("请输入顶点信息:");
for (i = 0; i < G->vexnum; i++) {
scanf(" %c", &G->vexs[i]);
}
for (i = 0; i < G->vexnum; i++) {
for (j = 0; j < G->vexnum; j++) {
G->arcs[i][j] = 0;
}
}
printf("请输入每条边的两个顶点及其权值:\n");
for (k = 0; k < G->arcnum; k++) {
scanf("%d%d%d", &i, &j, &w);
G->arcs[i][j] = w;
G->arcs[j][i] = w;
}
printf("邻接矩阵:\n");
for (i = 0; i < G->vexnum; i++) {
for (j = 0; j < G->vexnum; j++) {
printf("%d ", G->arcs[i][j]);
}
printf("\n");
}
}
// 创建无向图的邻接表并打印
void createALGraph(ALGraph *G) {
int i, j, k;
ArcNode *p;
printf("请输入顶点数和边数:");
scanf("%d%d", &G->vexnum, &G->arcnum);
printf("请输入顶点信息:");
for (i = 0; i < G->vexnum; i++) {
scanf(" %c", &G->vertices[i].data);
G->vertices[i].first = NULL;
}
printf("请输入每条边的两个顶点:\n");
for (k = 0; k < G->arcnum; k++) {
scanf("%d%d", &i, &j);
p = (ArcNode *)malloc(sizeof(ArcNode));
p->adjvex = j;
p->next = G->vertices[i].first;
G->vertices[i].first = p;
p = (ArcNode *)malloc(sizeof(ArcNode));
p->adjvex = i;
p->next = G->vertices[j].first;
G->vertices[j].first = p;
}
printf("邻接表:\n");
for (i = 0; i < G->vexnum; i++) {
printf("%c: ", G->vertices[i].data);
p = G->vertices[i].first;
while (p) {
printf("%c ", G->vertices[p->adjvex].data);
p = p->next;
}
printf("\n");
}
}
// 深度优先遍历
void DFS(ALGraph *G, int v, int visited[]) {
ArcNode *p;
visited[v] = 1;
printf("%c ", G->vertices[v].data);
p = G->vertices[v].first;
while (p) {
if (!visited[p->adjvex]) {
DFS(G, p->adjvex, visited);
}
p = p->next;
}
}
// 广度优先遍历
void BFS(ALGraph *G, int v, int visited[]) {
int queue[MAX_ARC_NUM], front = 0, rear = 0;
ArcNode *p;
visited[v] = 1;
printf("%c ", G->vertices[v].data);
queue[rear++] = v;
while (front < rear) {
v = queue[front++];
p = G->vertices[v].first;
while (p) {
if (!visited[p->adjvex]) {
visited[p->adjvex] = 1;
printf("%c ", G->vertices[p->adjvex].data);
queue[rear++] = p->adjvex;
}
p = p->next;
}
}
}
// 主函数
int main() {
int op;
MGraph G1;
ALGraph G2;
int visited[MAX_VERTEX_NUM] = {0};
while (1) {
printf("\n\n");
printf("1. 创建无向图的邻接矩阵并打印\n");
printf("2. 创建无向图的邻接表并打印\n");
printf("3. 无向图的深度优先遍历\n");
printf("4. 无向图的广度优先遍历\n");
printf("5. 退出程序\n");
printf("请选择操作:");
scanf("%d", &op);
switch (op) {
case 1:
createMGraph(&G1);
break;
case 2:
createALGraph(&G2);
break;
case 3:
printf("请输入遍历起始点:");
scanf("%d", &op);
DFS(&G2, op, visited);
printf("\n");
break;
case 4:
printf("请输入遍历起始点:");
scanf("%d", &op);
BFS(&G2, op, visited);
printf("\n");
break;
case 5:
exit(0);
default:
printf("无效操作,请重新选择!\n");
}
printf("按任意键继续...");
getch();
}
return 0;
}
```
在该程序中,我们使用了菜单来实现不同的功能,并且分别实现了无向图的邻接矩阵和邻接表的创建,以及深度优先遍历和广度优先遍历。
阅读全文