编写一个能够创建4种不同类型图的邻接表存储的通用函数,函数格式如下: CreateALGraph(GraphKind GKind, ALGraph &G, int vexnum, int arcnum, char *vexs, int *arcs)这个函数的main函数实现效果举例
时间: 2024-03-19 14:42:11 浏览: 70
好的,我明白了。以下是CreateALGraph函数的实现及其main函数的使用示例:
```c++
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
// 定义图的类型
enum GraphKind {
DG, // 有向图
DN, // 有向网
UDG, // 无向图
UDN // 无向网
};
// 边结点的类型
struct ArcNode {
int adjvex; // 该边所指向的顶点的位置
int weight; // 权值
ArcNode *nextarc; // 指向下一条边的指针
};
// 顶点结点的类型
struct VNode {
char data; // 顶点的数据域
ArcNode *firstarc; // 指向第一条边的指针
};
// 图的类型(邻接表存储)
struct ALGraph {
VNode *vexs; // 存储顶点的数组
int vexnum; // 顶点数
int arcnum; // 边数
GraphKind kind; // 图的类型
};
// 创建有向图
void CreateDG(ALGraph &G, int vexnum, int arcnum, char *vexs, int *arcs) {
G.vexs = new VNode[vexnum];
G.vexnum = vexnum;
G.arcnum = arcnum;
G.kind = DG;
int i, j;
// 初始化顶点
for (i = 0; i < vexnum; i++) {
G.vexs[i].data = vexs[i];
G.vexs[i].firstarc = NULL;
}
// 插入边
for (i = 0; i < arcnum; i++) {
int tail = arcs[i * 2];
int head = arcs[i * 2 + 1];
ArcNode *arc = new ArcNode;
arc->adjvex = head;
arc->nextarc = G.vexs[tail].firstarc;
G.vexs[tail].firstarc = arc;
}
}
// 创建有向网
void CreateDN(ALGraph &G, int vexnum, int arcnum, char *vexs, int *arcs) {
G.vexs = new VNode[vexnum];
G.vexnum = vexnum;
G.arcnum = arcnum;
G.kind = DN;
int i, j;
// 初始化顶点
for (i = 0; i < vexnum; i++) {
G.vexs[i].data = vexs[i];
G.vexs[i].firstarc = NULL;
}
// 插入边
for (i = 0; i < arcnum; i++) {
int tail = arcs[i * 3];
int head = arcs[i * 3 + 1];
int weight = arcs[i * 3 + 2];
ArcNode *arc = new ArcNode;
arc->adjvex = head;
arc->weight = weight;
arc->nextarc = G.vexs[tail].firstarc;
G.vexs[tail].firstarc = arc;
}
}
// 创建无向图
void CreateUDG(ALGraph &G, int vexnum, int arcnum, char *vexs, int *arcs) {
G.vexs = new VNode[vexnum];
G.vexnum = vexnum;
G.arcnum = arcnum;
G.kind = UDG;
int i, j;
// 初始化顶点
for (i = 0; i < vexnum; i++) {
G.vexs[i].data = vexs[i];
G.vexs[i].firstarc = NULL;
}
// 插入边
for (i = 0; i < arcnum; i++) {
int tail = arcs[i * 2];
int head = arcs[i * 2 + 1];
// 插入tail到head的边
ArcNode *arc1 = new ArcNode;
arc1->adjvex = head;
arc1->nextarc = G.vexs[tail].firstarc;
G.vexs[tail].firstarc = arc1;
// 插入head到tail的边
ArcNode *arc2 = new ArcNode;
arc2->adjvex = tail;
arc2->nextarc = G.vexs[head].firstarc;
G.vexs[head].firstarc = arc2;
}
}
// 创建无向网
void CreateUDN(ALGraph &G, int vexnum, int arcnum, char *vexs, int *arcs) {
G.vexs = new VNode[vexnum];
G.vexnum = vexnum;
G.arcnum = arcnum;
G.kind = UDN;
int i, j;
// 初始化顶点
for (i = 0; i < vexnum; i++) {
G.vexs[i].data = vexs[i];
G.vexs[i].firstarc = NULL;
}
// 插入边
for (i = 0; i < arcnum; i++) {
int tail = arcs[i * 3];
int head = arcs[i * 3 + 1];
int weight = arcs[i * 3 + 2];
// 插入tail到head的边
ArcNode *arc1 = new ArcNode;
arc1->adjvex = head;
arc1->weight = weight;
arc1->nextarc = G.vexs[tail].firstarc;
G.vexs[tail].firstarc = arc1;
// 插入head到tail的边
ArcNode *arc2 = new ArcNode;
arc2->adjvex = tail;
arc2->weight = weight;
arc2->nextarc = G.vexs[head].firstarc;
G.vexs[head].firstarc = arc2;
}
}
// 创建图的通用函数
void CreateALGraph(GraphKind GKind, ALGraph &G, int vexnum, int arcnum, char *vexs, int *arcs) {
switch (GKind) {
case DG: CreateDG(G, vexnum, arcnum, vexs, arcs); break;
case DN: CreateDN(G, vexnum, arcnum, vexs, arcs); break;
case UDG: CreateUDG(G, vexnum, arcnum, vexs, arcs); break;
case UDN: CreateUDN(G, vexnum, arcnum, vexs, arcs); break;
default: break;
}
}
// 测试函数
void PrintALGraph(ALGraph G) {
int i, j;
cout << "顶点数:" << G.vexnum << endl;
cout << "边数:" << G.arcnum << endl;
cout << "图的类型:";
switch (G.kind) {
case DG: cout << "有向图" << endl; break;
case DN: cout << "有向网" << endl; break;
case UDG: cout << "无向图" << endl; break;
case UDN: cout << "无向网" << endl; break;
default: break;
}
for (i = 0; i < G.vexnum; i++) {
cout << "顶点" << i << ":" << G.vexs[i].data << endl;
ArcNode *arc = G.vexs[i].firstarc;
while (arc != NULL) {
cout << " -> 边<" << i << ", " << arc->adjvex << ">";
if (G.kind == DN || G.kind == UDN) {
cout << ",权值:" << arc->weight;
}
cout << endl;
arc = arc->nextarc;
}
}
}
// 测试
int main() {
int vexnum = 5;
int arcnum = 7;
char vexs[5] = {'A', 'B', 'C', 'D', 'E'};
int arcs1[14] = {0, 1, 0, 2, 1, 2, 1, 3, 2, 4, 3, 4, 4, 0};
int arcs2[21] = {0, 1, 1, 2, 2, 3, 3, 4, 4, 0, 0, 2, 1, 3, 2, 4, 3, 0, 4, 1, 2};
int arcs3[21] = {0, 1, 1, 2, 2, 3, 3, 4, 4, 0, 2, 0, 3, 1, 4, 1, 2, 3, 2, 4, 3};
int arcs4[30] = {0, 1, 2, 1, 2, 1, 3, 3, 2, 3, 4, 4, 0, 4, 1, 5, 2, 5, 3, 5, 4, 6, 0, 6, 2, 6, 4, 6, 5};
ALGraph G1, G2, G3, G4;
CreateALGraph(DG, G1, vexnum, arcnum, vexs, arcs1);
CreateALGraph(UDG, G2, vexnum, arcnum, vexs, arcs2);
CreateALGraph(DN, G3, vexnum, arcnum, vexs, arcs3);
CreateALGraph(UDN, G4, vexnum, arcnum, vexs, arcs4);
PrintALGraph(G1);
PrintALGraph(G2);
PrintALGraph(G3);
PrintALGraph(G4);
return 0;
}
```
这个程序实现了一个通用的函数CreateALGraph,可以根据不同的图的类型创建邻接表存储的图。它的输入参数包括图的类型、图的结构信息(顶点数、边数、顶点数组和边数组),以及一个空的图对象,输出结果是一个存储了图结构的图对象。main函数中,我们使用了四个不同的图类型,测试了CreateALGraph函数的正确性。
阅读全文