邻接表的FirstAdjVex(G,u)的代码
时间: 2024-05-16 12:15:00 浏览: 10
假设邻接表的结构体定义如下:
```c++
#define MaxVertexNum 100 // 最大顶点数目
typedef char VertexType; // 顶点数据类型
typedef int EdgeType; // 边权重数据类型
// 邻接表的边表结点
typedef struct ArcNode
{
int adjvex; // 该边所指向的顶点位置
struct ArcNode *next; // 指向下一条边的指针
EdgeType weight; // 边权重
} ArcNode;
// 邻接表的顶点表结构
typedef struct VNode
{
VertexType data; // 顶点数据
ArcNode *firstarc; // 指向第一条依附该顶点的边的指针
} VNode, AdjList[MaxVertexNum];
// 整个邻接表结构
typedef struct
{
AdjList vertices; // 邻接表
int vexnum, arcnum; // 顶点数和边数
} ALGraph;
```
则邻接表的 `FirstAdjVex(G,u)` 函数可以实现如下:
```c++
// 返回邻接表G中顶点u的第一个邻接顶点的位置
int FirstAdjVex(ALGraph G, int u)
{
if (G.vertices[u].firstarc != nullptr)
{
return G.vertices[u].firstarc->adjvex;
}
return -1; // 如果没有邻接顶点,返回-1
}
```
其中,`G.vertices[u].firstarc` 指向以顶点 `u` 为弧尾的第一条边,`G.vertices[u].firstarc->adjvex` 返回这条边的弧头所对应的顶点位置。如果没有邻接顶点,则返回 `-1`。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)