完善下列代码 #include<iostream> #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MVNum 100 //最大顶点数 using namespace std; typedef struct {//图的邻接矩阵存储表示 int vexs[MVNum]; //顶点表 int arcs[MVNum][MVNum]; //邻接矩阵 int vexnum,arcnum; //图的当前点数和边数 }AMGragh; int CreateUDN(AMGragh &G,int vexnum,int arcnum) {//采用邻接矩阵表示法,创建无向网G } int InsertVex(AMGragh &G) {//在以邻接矩阵形式存储的无向图G上插入顶点 } int OutputUDN(AMGragh G) {//输出图G }
时间: 2023-10-21 17:03:31 浏览: 177
#include<iostream>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MVNum 100 //最大顶点数
using namespace std;
typedef struct {//图的邻接矩阵存储表示
int vexs[MVNum]; //顶点表
int arcs[MVNum][MVNum]; //邻接矩阵
int vexnum,arcnum; //图的当前点数和边数
}AMGragh;
int CreateUDN(AMGragh &G,int vexnum,int arcnum) {//采用邻接矩阵表示法,创建无向网G
//初始化顶点数和边数
G.vexnum = vexnum;
G.arcnum = arcnum;
//初始化邻接矩阵
for(int i = 0; i < G.vexnum; i++) {
for(int j = 0; j < G.vexnum; j++) {
G.arcs[i][j] = 0;
}
}
//输入顶点信息
cout << "请输入" << G.vexnum << "个顶点信息:";
for(int i = 0; i < G.vexnum; i++) {
cin >> G.vexs[i];
}
//输入边信息
cout << "请输入" << G.arcnum << "条边信息(起点 终点 权值):";
for(int k = 0; k < G.arcnum; k++) {
int i,j,w;
cin >> i >> j >> w;
G.arcs[i-1][j-1] = w;
G.arcs[j-1][i-1] = w;//因为是无向图,所以需要对称存储
}
return OK;
}
int InsertVex(AMGragh &G) {//在以邻接矩阵形式存储的无向图G上插入顶点
if(G.vexnum == MVNum) {
return OVERFLOW;//超出最大顶点数
}
//输入新顶点信息
cout << "请输入新插入的顶点信息:";
cin >> G.vexs[G.vexnum];
G.vexnum++;//顶点数加一
//初始化邻接矩阵
for(int i = 0; i < G.vexnum; i++) {
G.arcs[i][G.vexnum-1] = 0;
G.arcs[G.vexnum-1][i] = 0;
}
return OK;
}
int OutputUDN(AMGragh G) {//输出图G
cout << "顶点表:" << endl;
for(int i = 0; i < G.vexnum; i++) {
cout << G.vexs[i] << " ";
}
cout << endl << "邻接矩阵:" << endl;
for(int i = 0; i < G.vexnum; i++) {
for(int j = 0; j < G.vexnum; j++) {
cout << G.arcs[i][j] << " ";
}
cout << endl;
}
return OK;
}
阅读全文