题目:
图的邻接表和邻接矩阵存储
建立下图的邻接表或邻接矩阵,并输出之;
思路:
邻接矩阵:
(1)建立图结构,包含顶点数组,边数组,顶点和边总数
(2)定位函数:可以通过顶点的信息定位到顶点下标
(3)建立邻接矩阵函数:a.输入顶点和边总数;
b.录入顶点数组和边数组信息 邻接表: (1)建立图结构(顶点结构数组,顶点和边总数),边结构(边的顶点,权重,连接指针),顶点结构(顶点信息,边连接指针)
(2)定位函数:可以通过顶点的信息定位到顶点下标
(3)建立邻接图函数:
a.输入顶点和边总数;
b.录入顶点数组和边数组信息
c.利用前插法将边与顶点连接
代码块:
#include "pch.h"
#include <iostream>
using namespace std;
#define MVNum 100
typedef char VerTexType;//顶点数据类型设置为字符
typedef int ArcType;//边的权值为整型
typedef int OtherInfo;
/*********邻接矩阵***********/
struct AMGraph {
VerTexType vexs[MVNum];//顶点表
ArcType arcs[MVNum][MVNum];//邻接矩阵
int vexnum;//顶点数量
int arcnum;//边数量
};
int Locatevex(AMGraph G, VerTexType v)//定位顶点下标
{
for (int i = 0; i < G.vexnum; i++)//遍历寻找对应顶点下标
{
if (G.vexs[i]==v)
{
return i;
}
}
cout << "顶点输入错误,请重新检查!" << endl;//顶点输入错误处理
return -1;
}
void CreateUDN(AMGraph&G)
{
cout << "请依次输入总顶点数和总边数:";
cin >> G.vexnum >> G.arcnum;
cout << "请输入各顶点信息:" << endl;
for (int i = 0; i < G.vexnum; i++)//请输入各顶点信息
{
cin >> G.vexs[i];
}
for (int i = 0; i < G.vexnum; i++)//将邻接矩阵初始化为0
{
for (int j = 0; j < G.vexnum; j++)
{
G.arcs[i][j] = 0;
}
}
for (int k = 0; k < G.arcnum; k++)
{
VerTexType v1, v2;
int w;
cout << "请依次输第入"<<k+1<<"条边的两个顶点及权值:";
cin >> v1 >> v2 >> w;
int i = Locatevex(G, v1);//找到顶点在图中边的下标
int j = Locatevex(G, v2);
if (i != -1 && j != -1)//如果能找到对应顶点,对边赋权值
{
G.arcs[i][j] = w;
G.arcs[j][i] = w;
}
}
}
/*********邻接表***********/
struct ArcNode {
int adjvex;//连接的顶点下标
struct ArcNode *nextarc;//下一节点
OtherInfo info;//权值
};
struct VNode{
VerTexType data;//顶点数据
ArcNode *firstarc;//边的连接点
};
struct ALGraph {
VNode vertices[MVNum];//顶点数组
int vexnum;//顶点总数
int arcnum;//边总数
};
int Locatevex(ALGraph G, VerTexType v)//定位顶点下标
{
for (int i = 0; i < G.vexnum; i++)//遍历寻找对应顶点下标
{
if (G.vertices[i].data == v)
{
return i;
}
}
cout << "顶点输入错误,请重新检查!" << endl;//顶点输入错误处理
return -1;
}
void CreateUDG(ALGraph &G)
{
cout << "请依次输入总顶点数和总边数:";
cin >> G.vexnum >> G.arcnum;
cout << "请输入各顶点信息:" << endl;
for (int i = 0; i < G.vexnum; i++)//请输入各顶点信息
{
cin >> G.vertices[i].data;
G.vertices[i].firstarc = NULL;
}
for (int k = 0; k < G.arcnum; k++)
{
VerTexType v1, v2;
int w;
cout << "请依次输第入" << k << "条边的两个顶点及权值:";
cin >> v1 >> v2 >> w;
int i = Locatevex(G, v1);//找到顶点在图中边的下标
int j = Locatevex(G, v2);
ArcNode *p1 =new ArcNode;//边用前插法插入到顶点后方
p1->info = w;
p1->adjvex = i;
p1->nextarc = G.vertices[j].firstarc;
G.vertices[j].firstarc= p1;
ArcNode *p2 = new ArcNode;
p2->info = w;
p2->adjvex = j;
p2->nextarc = G.vertices[i].firstarc;
G.vertices[i].firstarc = p2;
}
}
int main()
{
/*********邻接矩阵***********/
cout << "/*********邻接矩阵***********/" << endl;
AMGraph G;
CreateUDN(G);
for (int i = 0; i < G.vexnum; i++)
cout << '\t' << "v" << i;
cout << endl;
for (int i = 0; i < G.vexnum; i++)//输出邻接矩阵
{
cout << "v" << i<<'\t';
for (int j = 0; j < G.vexnum; j++)
{
cout << G.arcs[i][j] << '\t';
}
cout << endl;
}
/*********邻接表***********/
cout << "/*********邻接表***********/" << endl;
ALGraph g;
CreateUDG(g);
for (int i = 0; i < g.vexnum; i++)//输出邻接表
{
cout << "第" << i + 1 << "个顶点:" << g.vertices[i].data << endl;
int k = 1;
ArcNode *p = g.vertices[i].firstarc;
while (p)
{
cout << "第" << i + 1 << "个顶点的" << "第" << k << "条边顶点:" << g.vertices[p->adjvex].data << '\t' << "权重:" << p->info << endl;
p = p->nextarc;
k++;
}
}
}
效果图:
来源:CSDN
作者:Sinb妃
链接:https://blog.csdn.net/weixin_43981315/article/details/103915371