本来在vc++6.0运行正常,但在codeblocks编译就报错,实在是没辙了,求解
#include <stdio.h>#include <malloc.h>
#define MAXV 100
#define INF 32767
typedef int InfoType; /*最大顶点个数*/
/*以下定义邻接矩阵类型*/
typedef struct
{ int no; /*顶点编号*/
InfoType info; /*顶点其他信息*/
} VertexType; /*顶点类型*/
typedef struct /*图的定义*/
{ int edges[MAXV][MAXV]; /*邻接矩阵*/
int vexnum,arcnum; /*顶点数,弧数*/
VertexType vexs[MAXV]; /*存放顶点信息*/
} MGraph; /*图的邻接矩阵类型*/
/*以下定义邻接表类型*/
typedef struct ANode /*弧的结点结构类型*/
{ int adjvex; /*该弧的终点位置*/
struct ANode *nextarc; /*指向下一条弧的指针*/
InfoType info; /*该弧的相关信息,这里用于存放权值*/
} ArcNode;
typedef int Vertex;
typedef struct Vnode /*邻接表头结点的类型*/
{ Vertex data; /*顶点信息*/
ArcNode *firstarc; /*指向第一条弧*/
} VNode;
typedef VNode AdjList[MAXV]; /*AdjList是邻接表类型*/
typedef struct
{ AdjList adjlist; /*邻接表*/
int n,e; /*图中顶点数n和边数e*/
} ALGraph; /*图的邻接表类型*/
void MatToList(MGraph g,ALGraph *&G)//这里也报错。。。
{ int i,j,n=g.vexnum; /*n为顶点数*/
ArcNode *p;
G=(ALGraph *)malloc(sizeof(ALGraph));
for (i=0;i<n;i++) /*给邻接表中所有头结点的指针域置初值*/
G->adjlist[i].firstarc=NULL;
for (i=0;i<n;i++) /*检查邻接矩阵中每个元素*/
for (j=n-1;j>=0;j--)
if (g.edges[i][j]!=0) /*邻接矩阵的当前元素不为0*/
{
p=(ArcNode *)malloc(sizeof(ArcNode)); /*创建一个结点*p*/
p->adjvex=j;
p->info=g.edges[i][j];
p->nextarc=G->adjlist[i].firstarc; /*将*p链到链表后*/
G->adjlist[i].firstarc=p;
}
G->n=n;G->e=g.arcnum;
}
void ListToMat(ALGraph *G,MGraph &g)////////就是这里报错。
{ int i,j,n=G->n;
ArcNode *p;
for (i=0;i<n;i++) /*g.edges[i][j]赋初值0*/
for (j=0;j<n;j++)
g.edges[i][j]=0;
for (i=0;i<n;i++)
{
p=G->adjlist[i].firstarc;
while (p!=NULL)
{
g.edges[i][p->adjvex]=p->info;
p=p->nextarc;
}
}
g.vexnum=n;g.arcnum=G->e;
}
void DispMat(MGraph g)
/*输出邻接矩阵g*/
{ int i,j;
for (i=0;i<g.vexnum;i++)
{
for (j=0;j<g.vexnum;j++)
if (g.edges[i][j]==INF)
printf("%3s","∞");
else
printf("%3d",g.edges[i][j]);
printf("\n");
}
}
void DispAdj(ALGraph *G)
{ int i;
ArcNode *p;
for (i=0;i<G->n;i++)
{
p=G->adjlist[i].firstarc;
if (p!=NULL) printf("%3d: ",i);
while (p!=NULL)
{
printf("%3d",p->adjvex);
p=p->nextarc;
}
printf("\n");
}
}
int main()
{ int i,j;
MGraph g,g1;
ALGraph *G;
int A[MAXV][6]={ {0,5,0,7,0,0},{0,0,4,0,0,0},{8,0,0,0,0,9},{0,0,5,0,0,6}, {0,0,0,5,0,0},
{3,0,0,0,1,0}};
g.vexnum=6;g.arcnum=10;
for (i=0;i<g.vexnum;i++) /*建立如图的邻接矩阵*/
for (j=0;j<g.vexnum;j++)
g.edges[i][j]=A[i][j];
printf("\n");
printf(" 有向图G的邻接矩阵:\n");
DispMat(g);
G=(ALGraph *)malloc(sizeof(ALGraph));
printf(" 图G的邻接矩阵转换成邻接表:\n");
MatToList(g,G);
DispAdj(G);
printf(" 图G的邻接表转换成邻接邻阵:\n");
ListToMat(G,g1);
DispMat(g1);
printf("\n");
return 0;
}
\g\main.c|33|error: expected ';', ',' or ')' before '&' token|
\g\main.c|51|error: expected ';', ',' or ')' before '&' token|