当前位置:网站首页>08.02 邻接表
08.02 邻接表
2022-07-26 21:26:00 【zzyzxb】
一:知识点概述
























//创建有向图的邻接表
#include <iostream>
using namespace std;
const int MaxVnum = 100;//顶点数最大值
typedef char VexType;//顶点的数据类型为字符型
typedef struct AdjNode {
//定义邻接点类型
int v; //邻接点下标
struct AdjNode* next; //指向下一个邻接点
}AdjNode;
typedef struct VexNode {
//定义顶点类型
VexType data; // VexType为顶点的数据类型,根据需要定义
AdjNode* first; //指向第一个邻接点
}VexNode;
typedef struct {
//定义邻接表类型
VexNode Vex[MaxVnum];
int vexnum, edgenum; //顶点数,边数
}ALGragh;
int locatevex(ALGragh G, VexType x)
{
for (int i = 0; i < G.vexnum; i++) //查找顶点信息的下标
if (x == G.Vex[i].data)
return i;
return -1;//没找到
}
void insertedge(ALGragh& G, int i, int j) //插入一条边
{
AdjNode* s;
s = new AdjNode;
s->v = j;
s->next = G.Vex[i].first;
G.Vex[i].first = s;
}
void printg(ALGragh G)//输出邻接表
{
cout << "----------邻接表如下:----------" << endl;
for (int i = 0; i < G.vexnum; i++)
{
AdjNode* t = G.Vex[i].first;
cout << G.Vex[i].data << ": ";
while (t != NULL)
{
cout << "[" << t->v << "] ";
t = t->next;
}
cout << endl;
}
}
void CreateALGraph(ALGragh& G) //创建有向图邻接表
{
int i, j;
VexType u, v;
cout << "请输入顶点数和边数:" << endl;
cin >> G.vexnum >> G.edgenum;
cout << "请输入顶点信息:" << endl;
for (i = 0; i < G.vexnum; i++)//输入顶点信息,存入顶点信息数组
cin >> G.Vex[i].data;
for (i = 0; i < G.vexnum; i++)
G.Vex[i].first = NULL;
cout << "请依次输入每条边的两个顶点u,v" << endl;
while (G.edgenum--)
{
cin >> u >> v;
i = locatevex(G, u); //查找顶点u的存储下标
j = locatevex(G, v); //查找顶点v的存储下标
if (i != -1 && j != -1)
insertedge(G, i, j);
else
{
cout << "输入顶点信息错!请重新输入!" << endl;
G.edgenum++; //本次输入不算
}
}
}
int main()
{
ALGragh G;
CreateALGraph(G); //创建有向图邻接表
printg(G); //输出邻接表
return 0;
}
边栏推荐
- In depth analysis of the source code, why is the string class immutable? (hit me before you understand)
- 梦里的一碗面
- Unity对资源管理器操作 打开资源管理器选择文件并筛选文件
- If you do not add waitkey() function after imshow() function, it will not be displayed
- Altium designer 22 Chinese character garbled
- SQL注入 Less26(过滤空格和注释符,使用不带空格的报错注入)
- easyui datagrid 获取多条选中的数据进行操作
- 月薪5万的朋友告诉我,你只是在打杂
- Altium designer 22 modify the layer properties of the selected component
- 软件测试技术之跨平台的移动端UI自动化测试(下)
猜你喜欢

带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?

深入源码剖析String类为什么不可变?(还不明白就来打我)

A bowl of noodles in a dream

Flash source code startup phase

Isilon's onefs common operation commands (I)

Resume in 2022 is dead in the sea. Don't vote. Software testing positions are saturated

Let Xiaobai thoroughly understand performance tuning

LDAP -- realize unified login management of users

08 du 命令

小白学习MySQL - Derived Table
随机推荐
Difference between D and C
Implementation of MATLAB short-time autocorrelation
一篇让小百彻底搞懂性能调优
基于CAShapeLayer和贝塞尔曲线的圆形进度条动画
ansible安装及使用
ZABBIX calls API retrieval method
My SQL is OK. Why is it still so slow? MySQL locking rules
学了那么久的用例设计,知不知道为什么设计测试用例
Go----Go 语言命名规范
I successfully landed the automatic testing post, with a maximum monthly salary of 15.4k. I'm great~
Basic operation of (C language) files
梦里的一碗面
Cmake compiling obs-studio-27.2.0
Resume in 2022 is dead in the sea. Don't vote. Software testing positions are saturated
Just one dependency to give swagger a new skin, which is simple and cool~
Flink's real-time data analysis practice in iFLYTEK AI marketing business
: could not determine a constructor for the tag !RootAdmin
NPM, NPM Chinese documents, NPM learning and using
带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
What you need to know about mobile video compatibility