将这段代码修改为正确的格式 #include <stdio.h>#include <stdlib.h>#include <conio.h>#define MAX 20typedef int VexType;typedef int WeightType; // 边的权值类型typedef struct { VexType vex; // 顶点数据 WeightType w; // 边的权值} Edge;typedef struct { VexType vexs[MAX]; // 顶点数组 Edge edges[MAX * MAX]; // 边数组 int n, e; // 顶点数和边数} MGraph;void creat_mg(MGraph *G);void out_mg(MGraph *G);MGraph G1;int main(){ creat_mg(&G1); out_mg(&G1); return 0;}void creat_mg(MGraph *G){ int i, j, k, w; printf("\n n,e=?"); scanf("%d%d", &(G->n), &(G->e)); for (i = 0; i < G->n; i++) { printf("\n 请输入第%d个顶点的数据: ", i + 1); scanf("%d", &(G->vexs[i])); } for (k = 0; k < G->e; k++) { printf("\n 请输入第%d条边的起点、终点和权值: ", k + 1); scanf("%d%d%d", &i, &j, &w); G->edges[k].vex = j - 1; G->edges[k].w = w; G->edges[k].next = G->vexs[i - 1].firstedge; G->vexs[i - 1].firstedge = &(G->edges[k]); }}void out_mg(MGraph *G){ int i, j; Edge *e; printf("\n邻接表如下:\n"); for (i = 0; i < G->n; i++) { printf("%d: ", G->vexs[i].vex); e = G->vexs[i].firstedge; while (e != NULL) { printf("%d(%d) ", e->vex, e->w); e = e->next; } printf("\n"); } printf("\n存在的边如下:\n"); for (i = 0; i < G->n; i++) { e = G->vexs[i].firstedge; while (e != NULL) { j = e->vex; printf("%d %d %d\n", G->vexs[i].vex, G->vexs[j].vex, e->w); e = e->next; } } printf("\n\n 打回车键,继续。"); getch();}
时间: 2024-04-27 21:19:37 浏览: 100
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 20
typedef int VexType;
typedef int WeightType; // 边的权值类型
typedef struct Edge {
int vex; // 顶点数据
WeightType w; // 边的权值
struct Edge *next; // 下一条边
} Edge;
typedef struct {
VexType vexs[MAX]; // 顶点数组
Edge edges[MAX * MAX]; // 边数组
int n, e; // 顶点数和边数
} MGraph;
void creat_mg(MGraph *G);
void out_mg(MGraph *G);
MGraph G1;
int main(){
creat_mg(&G1);
out_mg(&G1);
return 0;
}
void creat_mg(MGraph *G){
int i, j, k, w;
printf("\n n,e=?");
scanf("%d%d", &(G->n), &(G->e));
for (i = 0; i < G->n; i++) {
printf("\n 请输入第%d个顶点的数据: ", i + 1);
scanf("%d", &(G->vexs[i]));
}
for (k = 0; k < G->e; k++) {
printf("\n 请输入第%d条边的起点、终点和权值: ", k + 1);
scanf("%d%d%d", &i, &j, &w);
G->edges[k].vex = j - 1;
G->edges[k].w = w;
G->edges[k].next = G->vexs[i - 1].firstedge;
G->vexs[i - 1].firstedge = &(G->edges[k]);
}
}
void out_mg(MGraph *G){
int i, j;
Edge *e;
printf("\n邻接表如下:\n");
for (i = 0; i < G->n; i++) {
printf("%d: ", G->vexs[i]);
e = G->vexs[i].firstedge;
while (e != NULL) {
printf("%d(%d) ", e->vex, e->w);
e = e->next;
}
printf("\n");
}
printf("\n存在的边如下:\n");
for (i = 0; i < G->n; i++) {
e = G->vexs[i].firstedge;
while (e != NULL) {
j = e->vex;
printf("%d %d %d\n", G->vexs[i], G->vexs[j], e->w);
e = e->next;
}
}
printf("\n\n 打回车键,继续。");
getch();
}
阅读全文