#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 }补全代码
时间: 2023-10-21 21:07:20 浏览: 207
下面是补全代码的示例:
```
#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函数用于输出无向图的邻接表表示。
阅读全文