int InsertVex(AMGragh &G) {//在以邻接矩阵形式存储的无向图G上插入顶点 /**************begin************/ if(G.vexnum+1>MVNum)return error; int x; cin>>x; G.vexnum++; G.vexs[0][G.vexnum]=G.vexs[G.vexnum][0]=x; for(int i=1;i<=G.vexnum;i++) G.vexs[G.vexnum][i]=G.vexs[i][G.vexnum]=0; return ok;改正代码
时间: 2023-09-08 19:11:15 浏览: 20
int InsertVex(AMGraph &G) {
if(G.vexnum+1 > MAXVEX) return ERROR; // 判断是否超过最大顶点数
int x;
cin >> x;
G.vexnum++;
G.vexs[G.vexnum-1] = x; // 直接将新顶点存入末尾
for(int i = 0; i < G.vexnum-1; i++) {
G.arcs[i][G.vexnum-1] = G.arcs[G.vexnum-1][i] = 0; // 新顶点与其他点之间的边权值初始化为0
}
return OK;
}
相关问题
完善下列代码 #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 }
#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;
}
#include<iostream> #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MVNum 100 //最大顶点数 using namespace std; typedef struct ArcNode {//边结点 int adjvex; //邻接点域:该边所指向的顶点的位置 int data; //数据域:存储和边相关的信息 struct ArcNode* nextarc; //链域:指向下一条边的指针 }ArcNode; typedef struct VNode {//顶点信息 int data; //顶点结点的数据域 ArcNode *firstarc; //链域:指向第一条依附该顶点的边的指针 }VNode,AdjList[MVNum]; //AdjList表示邻接表类型 typedef struct {//邻接表 AdjList vertices; int vexnum,arcnum; //图的当前顶点数和边数 }ALGragh; int CreateUDG(ALGragh &G,int vexnum,int arcnum) {//采用邻接表表示法,创建无向图G } int InsertVex(ALGragh &G) {//在以邻接表形式存储的无向图G上插入顶点v } int PrintGraph(ALGragh G) {//输出图G }补全代码
下面是补全代码的示例:
```
#include<iostream>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MVNum 100 //最大顶点数
using namespace std;
typedef struct ArcNode {//边结点
int adjvex; //邻接点域:该边所指向的顶点的位置
int data; //数据域:存储和边相关的信息
struct ArcNode* nextarc; //链域:指向下一条边的指针
}ArcNode;
typedef struct VNode {//顶点信息
int data; //顶点结点的数据域
ArcNode *firstarc; //链域:指向第一条依附该顶点的边的指针
}VNode,AdjList[MVNum]; //AdjList表示邻接表类型
typedef struct {//邻接表
AdjList vertices;
int vexnum,arcnum; //图的当前顶点数和边数
}ALGragh;
int CreateUDG(ALGragh &G,int vexnum,int arcnum) {//采用邻接表表示法,创建无向图G
G.vexnum = vexnum;
G.arcnum = arcnum;
for(int i=0;i<G.vexnum;i++){
G.vertices[i].data = i+1; //顶点信息
G.vertices[i].firstarc = NULL; //边表头指针
}
for(int k=1;k<=G.arcnum;k++){
int i,j;
cout << "请输入第" << k << "条边的两个顶点的编号(空格分隔):" << endl;
cin >> i >> j;
//插入边<i,j>
ArcNode *p = new ArcNode;
p->adjvex = j-1;
p->nextarc = G.vertices[i-1].firstarc;
G.vertices[i-1].firstarc = p;
//插入边<j,i>
ArcNode *q = new ArcNode;
q->adjvex = i-1;
q->nextarc = G.vertices[j-1].firstarc;
G.vertices[j-1].firstarc = q;
}
return OK;
}
int InsertVex(ALGragh &G) {//在以邻接表形式存储的无向图G上插入顶点v
if(G.vexnum == MVNum){
cout << "图的顶点数已达到最大值,无法插入!" << endl;
return ERROR;
}
G.vexnum++;
G.vertices[G.vexnum-1].data = G.vexnum;
G.vertices[G.vexnum-1].firstarc = NULL; //边表头指针
return OK;
}
int PrintGraph(ALGragh G) {//输出图G
cout << "图的邻接表表示如下:" << endl;
for(int i=0;i<G.vexnum;i++){
cout << "顶点" << G.vertices[i].data << ": ";
ArcNode *p = G.vertices[i].firstarc;
while(p != NULL){
cout << p->adjvex+1 << "->";
p = p->nextarc;
}
cout << "NULL" << endl;
}
return OK;
}
int main(){
ALGragh G;
int vexnum,arcnum;
cout << "请输入无向图的顶点数和边数(空格分隔):" << endl;
cin >> vexnum >> arcnum;
CreateUDG(G,vexnum,arcnum);
PrintGraph(G);
InsertVex(G);
PrintGraph(G);
return 0;
}
```
其中,CreateUDG函数用于创建无向图,InsertVex函数用于在无向图中插入新的顶点,PrintGraph函数用于输出无向图的邻接表表示。
相关推荐














