ArcNode* pT = G.vertices[nStart].Firstarc;请解释这段代码
时间: 2024-03-04 14:54:11 浏览: 21
这段代码的作用是从图G中获取起点nStart的第一条边的指针,并将其赋值给指针变量pT。具体来说,G.vertices[nStart]表示图G中编号为nStart的顶点,其包含了一个指向第一条边的指针Firstarc。因此,G.vertices[nStart].Firstarc表示起点nStart的第一条边,类型为ArcNode结构体指针。将其赋值给pT后,就可以通过pT来访问起点nStart的第一条边的信息,例如pT->adjvex表示该边的终点。这种方式通常用于遍历图的邻接表表示法,通过顶点的指针访问其所有的出边或入边。
相关问题
怎么修改这个算法double GetWeight(ALGraph G, int nStart, int nEnd, char cKind) { double dTorM = INFINITY*1.0; if (cKind == 'T' || cKind == 'M') { if (cKind == 'T') { ArcNode* pT = G.vertices[nStart].Firstarc; for (; pT != NULL; pT = pT->nextArc) { if (pT->nAdjvex == nEnd) { if (pT->Information.dTime < dTorM) dTorM = pT->Information.dTime; } } } else{ ArcNode* pM = G.vertices[nStart].Firstarc; for (; pM != NULL; pM = pM->nextArc) { if (pM->nAdjvex == nEnd) { if (pM->Information.dCost < dTorM) dTorM = pM->Information.dCost; } } } return dTorM; } else { cout << "Input error! Please enter T or M." << endl; cout << " If Time please enter T. If Money please enter M." << endl; cin >> cKind; return GetWeight(G, nStart, nEnd, cKind); } }
可以按照以下方式修改算法:
1. 在输入参数时,添加一个循环,直到用户输入的参数为'T'或'M'为止。如果用户输入的不是这两个字符,则输出错误信息并要求用户重新输入,直到输入正确为止。
2. 在计算最小权值时,将dTorM的初值设为无穷大,以确保最小权值的准确性。
3. 在遍历邻接表的过程中,先判断邻接表是否为空,如果为空,则直接返回无穷大。如果不为空,则继续遍历邻接表。
修改后的代码如下:
```c++
double GetWeight(ALGraph G, int nStart, int nEnd, char cKind) {
double dTorM = numeric_limits<double>::infinity();
while (cKind != 'T' && cKind != 'M') {
cout << "Input error! Please enter T or M." << endl;
cout << "If Time please enter T. If Money please enter M." << endl;
cin >> cKind;
}
if (cKind == 'T') {
ArcNode* pT = G.vertices[nStart].Firstarc;
if (pT == NULL) {
return dTorM;
}
for (; pT != NULL; pT = pT->nextArc) {
if (pT->nAdjvex == nEnd) {
if (pT->Information.dTime < dTorM) {
dTorM = pT->Information.dTime;
}
}
}
}
else if (cKind == 'M') {
ArcNode* pM = G.vertices[nStart].Firstarc;
if (pM == NULL) {
return dTorM;
}
for (; pM != NULL; pM = pM->nextArc) {
if (pM->nAdjvex == nEnd) {
if (pM->Information.dCost < dTorM) {
dTorM = pM->Information.dCost;
}
}
}
}
return dTorM;
}
```
帮我修改一下以下代码int FirstAdjVertex(AdjList G,int v) { ArcNode *p=G.vertex[v].firstarc; if(p!=NULL) { return p->adjvex; } return -1; } int NextAdjVertex(AdjList G,int v,int w) { ArcNode *p=G.vertex[v].firstarc;; while(p!=NULL&&p->adjvex==w) { p=p->nextarc; } if(p==NULL) { return -1; } return p->nextarc->adjvex; }
以下是修改后的代码:
```c
int FirstAdjVertex(AdjList G, int v) {
ArcNode *p = G.vertex[v].firstarc;
if (p != NULL) {
return p->adjvex;
}
return -1;
}
int NextAdjVertex(AdjList G, int v, int w) {
ArcNode *p = G.vertex[v].firstarc;
while (p != NULL && p->adjvex != w) {
p = p->nextarc;
}
if (p == NULL || p->nextarc == NULL) {
return -1;
}
return p->nextarc->adjvex;
}
```
修改点:
1. 第二个函数中的 `while` 循环条件应该是 `p->adjvex != w`,表示找到当前节点的下一个节点。
2. 在第二个函数中,需要判断 `p->nextarc` 是否为 `NULL`,如果是,则说明当前节点是最后一个节点,返回值为 `-1`。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)