修改#include <iostream> using namespace std; #define MVNum 100 typedef int Status; typedef char VerTexType; typedef int ArcType; typedef int OtherInfo; typedef struct ArcNode{ int adjvex; struct ArcNode *nextarc; OtherInfo info; }ArcNode; typedef struct VNode{ VerTexType data; ArcNode *firstarc; }VNode,AdjList[MVNum]; typedef struct { AdjList vertices; //vertex的复数为vertices int vexnum,arcnum; }ALGraph; int LocateVex(ALGraph G,char u) { /* 初始条件: 图G存在,u和G中顶点有相同特征*/ /* 操作结果: 若G中存在顶点u,则返回该顶点在图中位置;否则返回-1 */ for(int i=0;i<G.vexnum;++i){ if(u==G.vertices[i].data){ return i; } } return -1; } Status CreateUDG(ALGraph &G){ int i,j,k; char v1,v2; cin>>G.vexnum>>G.arcnum; for(i=0;i<G.vexnum;++i){ cin>>G.vertices[i].data; G.vertices[i].firstarc=NULL; } for (k=0;k<G.arcnum;++k){ cin>>v1>>v2; i=LocateVex(G,v1); j=LocateVex(G,v2); ArcNode *p1,*p2; p1=new ArcNode;p1->adjvex=j; p1->nextarc=G.vertices[i].firstarc; G.vertices[i].firstarc=p1; p2=new ArcNode;p2->adjvex=i; p2->nextarc=G.vertices[j].firstarc; G.vertices[j].firstarc=p2; } return 0; } int main{ CreateUDG(ALGraph H) return 0; }
时间: 2024-04-09 18:28:18 浏览: 91
在你提供的代码中,有一些错误和不完整的地方。下面是修改后的代码:
```cpp
#include <iostream>
using namespace std;
#define MVNum 100
typedef int Status;
typedef char VerTexType;
typedef int ArcType;
typedef int OtherInfo;
typedef struct ArcNode {
int adjvex;
struct ArcNode* nextarc;
OtherInfo info;
} ArcNode;
typedef struct VNode {
VerTexType data;
ArcNode* firstarc;
} VNode, AdjList[MVNum];
typedef struct {
AdjList vertices; // vertex的复数为vertices
int vexnum, arcnum;
} ALGraph;
int LocateVex(ALGraph G, char u)
{
/* 初始条件: 图G存在,u和G中顶点有相同特征*/
/* 操作结果: 若G中存在顶点u,则返回该顶点在图中位置;否则返回-1 */
for (int i = 0; i < G.vexnum; ++i) {
if (u == G.vertices[i].data) {
return i;
}
}
return -1;
}
Status CreateUDG(ALGraph& G)
{
int i, j, k;
char v1, v2;
cin >> G.vexnum >> G.arcnum;
for (i = 0; i < G.vexnum; ++i) {
cin >> G.vertices[i].data;
G.vertices[i].firstarc = NULL;
}
for (k = 0; k < G.arcnum; ++k) {
cin >> v1 >> v2;
i = LocateVex(G, v1);
j = LocateVex(G, v2);
ArcNode* p1 = new ArcNode;
p1->adjvex = j;
p1->nextarc = G.vertices[i].firstarc;
G.vertices[i].firstarc = p1;
ArcNode* p2 = new ArcNode;
p2->adjvex = i;
p2->nextarc = G.vertices[j].firstarc;
G.vertices[j].firstarc = p2;
}
return 0;
}
int main()
{
ALGraph H;
CreateUDG(H);
return 0;
}
```
在修改后的代码中,我添加了main函数的括号,表示正确的函数定义。同时,我在main函数中创建了一个ALGraph类型的变量H,并且将H作为参数传递给CreateUDG函数。
这样修改后的代码就可以正确编译和运行了。如果还有其他问题,请随时提问。
阅读全文