// 图的邻接表数据结构C++描述
#include <iostream>
using namespace std;
const int MaxVexs=20;
//前向声明
template <typename ArcType, typename VexType> class Graph;
template <typename ArcType, typename VexType> class VexNode;
// 边节点类
template <typename ArcType>
class ArcNode{
template<typename Type1, typename Type2>friend class VexNode;
template<typename Type1, typename Type2>friend class Graph;
private:
int adjvex;
ArcType weight;
ArcNode *nextarc;
ArcNode():nextarc(NULL){
}
ArcNode(int v, const ArcType& w):adjvex(v),weight(w),nextarc(NULL){
}
};
//顶点节点类
template <typename ArcType, typename VexType>
class VexNode{
friend class Graph<ArcType, VexType>;
private:
VexType data;
ArcNode<ArcType> *firstarc;
VexNode():firstarc(NULL){
}
};
// 邻接表图类
template<typename ArcType, typename VexType>
class Graph{
private:
VexNode<ArcType,VexType> *VexTable;
int CurrentNumVexs;
int CurrentNumArcs;
//int GetVexPosition(const VexType& v);
public:
Graph():VexTable(NULL),CurrentNumVexs(0),CurrentNumArcs(0){
}
Graph(VexType *v, int num=MaxVexs);
~Graph();
bool empty() {
return CurrentNumVexs==0; }
int GetNumVexs() {
return CurrentNumVexs; }
带权图(网)的邻接表存储的C++实现
![](https://img-home.csdnimg.cn/images/20240711042549.png)