关于数据结构图的代码的解释templateGraphmtx::Graphmtx(int sz){//构造函数maxVertices = sz;numVertices = 0;numEdges = 0;int i,j;VerticesList = new T[maxVertices]; //创建顶点表数组Edge = (E**)new E*[maxVertices]; //创建邻接矩

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/12 19:27:45
关于数据结构图的代码的解释templateGraphmtx::Graphmtx(int sz){//构造函数maxVertices = sz;numVertices = 0;numEdges = 0;int i,j;VerticesList = new T[maxVertices]; //创建顶点表数组Edge = (E**)new E*[maxVertices]; //创建邻接矩

关于数据结构图的代码的解释templateGraphmtx::Graphmtx(int sz){//构造函数maxVertices = sz;numVertices = 0;numEdges = 0;int i,j;VerticesList = new T[maxVertices]; //创建顶点表数组Edge = (E**)new E*[maxVertices]; //创建邻接矩
关于数据结构图的代码的解释
template
Graphmtx::Graphmtx(int sz){
//构造函数
maxVertices = sz;numVertices = 0;numEdges = 0;
int i,j;
VerticesList = new T[maxVertices]; //创建顶点表数组
Edge = (E**)new E*[maxVertices]; //创建邻接矩阵数组
for(int i = 0; i

关于数据结构图的代码的解释templateGraphmtx::Graphmtx(int sz){//构造函数maxVertices = sz;numVertices = 0;numEdges = 0;int i,j;VerticesList = new T[maxVertices]; //创建顶点表数组Edge = (E**)new E*[maxVertices]; //创建邻接矩
这是创建二维数组的方法,我们把二维数组的每一行看成是一个元素的话,则这个二维数组就成了一维数组了,这个一维数组中的每个元素又是一个一维数组,根据这样的思路可以扩张到n维数组,而我们在创建一个简单的一维数组时(不妨称之为行数组)只要有一个元素指针,然后为这个元素指针new一个数组空间就可以了.那么同理,如果要new一个m行n列的二维数组,则先要有m个行数组的指针,把这m个行指针放在一个长度为m的指针数组中,所以就先new一个长度为m的指针数组了.再为这m个指针的每个指针new一个长度为n的行数组,则整个二维数组就创建好了.例如创建一个长度为4的一维int型数组可如下理解
typedef int* Row;
Row array = new int[4];
而若创建一个3行4列的int型数组,我们先把它们看成是一个长度为3的一维数组,其中每个元素为一个长度是4的int一维数组.所以先创建一个长度为3的行指针数组如下
Row* Array = new Row[3];
再为每个指针new一个int型行数组,可如下
Array[i] = new int[4];
整理这些语句就是
typedef int* Row;
Row* Array = new Row[3];
for(i = 0; i < 3; i++) Array[i] = new int[4];
将上述语句中的Row类型还原,则上述语句实际上就是
int** Array = new int*[3];
for(i = 0; i < 3; i++) Array[i] = new int[4];
而你提到的 ...= (E**)new …这是强制类型转换.
另外的
Edge[i][j] = (i == j) 0 ; maxWeight;
它的意思是如果i==j,即顶点i到自身的距离则初始化为0
如果i != j,即顶点i到其他顶点j的距离,则将其初始化为无穷大,计算机无法表示无穷大,就只好用一个非常大的数权且表示为无穷大.