CSDN 博主:住在阳光的心里
}
//邻接表法创建无向图
int CreateUDG(AGraph& G) {
int i, j, k, v1, v2;
ArcNode* p1, * p2;
printf("请输入总顶点数和总边数: ");
scanf_s("%d", &G.vexnum); //输入总顶点数
scanf_s("%d", &G.arcnum); //输入总边数
printf("\n");
printf("请输入顶点信息: ");
for (i = 0; i < G.vexnum; ++i) { //输入各点,构造表头节点表
scanf_s("%d", &G.vertices[i].data); //输入顶点值
G.vertices[i].firstarc = NULL; //初始化表头节点的指针域为 NULL
}
printf("\n");
printf("请输入每一条边对应的两个顶点: \n");
for (k = 0; k < G.arcnum; ++k) { //输入各边,构造邻接表
scanf_s("%d%d", &v1, &v2); //输入一条边依附的两个顶点
i = LocateVex(G, v1);
j = LocateVex(G, v2);
p1 = new ArcNode; //生成一个新的边节点*p1
p1->adjvex = j; //邻接点序号为 j
p1->nextarc = G.vertices[i].firstarc; //将新节点*p1 插入顶点 vi 的边表头
G.vertices[i].firstarc = p1;
p2 = new ArcNode; //生成一个新的边节点*p2
p2->adjvex = i; //邻接点序号为 i
p2->nextarc = G.vertices[j].firstarc; //将新节点*p2 插入顶点 vj 的边表头
G.vertices[j].firstarc = p2;
}
return OK;
}
//打印邻接表存储数据
void print_position(AGraph G) {
for (int i = 0; i < G.vexnum; i++) {
ArcNode p;
p.adjvex = G.vertices[i].firstarc->adjvex;
p.nextarc = G.vertices[i].firstarc->nextarc;
printf("%d -> ", G.vertices[i].data);
while (p.nextarc != NULL) {
printf("%d -> ", G.vertices[p.adjvex].data);