一个程序(只有一个错误:anonymous with no linkage to declare....)麻烦帮忙调试一下
这个程序主要内容:利用邻接表存储结构遍历图和网,并且利用邻接矩阵存储结构输出最小生成树(主要是这个部分出了问题)代码如下:#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define INT_MAX 1000
#define INFINITY INT_MAX
#define MAX_VERTEX_NUM 20
#define ERROR false
#define OK true
typedef struct ArcCell {
int adj;
char *info;
}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct {
char vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum, arcnum;
}MGraph;
typedef struct ArcNode{
int adjvex;
struct ArcNode *nextarc;
int *info;
}ArcNode;
typedef struct VNode{
char data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct{
AdjList vertices;
int vexnum,arcnum;
int kind;
}ALGraph;
//邻接矩阵 (udn)
int LocateVex(MGraph G, char u)
{
int i;
for (i=0; i<G.vexnum; ++i){
if (G.vexs[i]==u)
return i;
}
return -1;
}
int CreateUDN(MGraph &G) {
int IncInfo;
int i,j,k,w;
char v1,v2;
printf("(建立最小生成树)构建无向网,请输入顶点的数目和边的数目:\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("请依次输入对应的顶点的名称:\n");
for( i = 0; i < G.vexnum; ++i ) {
scanf("%c",&G.vexs[i]);
}
for( i = 0; i < G.vexnum; ++i ) {
for( j = 0; j <G.vexnum; ++j ) {
G.arcs[i][j].adj = INFINITY;
G.arcs[i][j].info = NULL;
}
}
for( k = 0; k < G.arcnum; ++k ) {
printf("请输入第%d条边的两个顶点的名称和相应的权:\n",k+1);
scanf("%c%c%d",&v1,&v2,&w);
i=LocateVex(G,v1);
j=LocateVex(G,v2);
G.arcs[i][j].adj=w;
printf("此边是否含有其他信息:(0、不是;1、是)\n");
scanf("%d",&IncInfo);
if(IncInfo)
scanf("%s",&G.arcs[i][j].info);
G.arcs[j][i]=G.arcs[i][j];
}
return OK;
}
//邻接表
int CreateUDG2(ALGraph &G){
int i,s,d;
ArcNode *p,*q;
printf("请输入顶点数,边数:\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("请输入顶点值(字符)\n");
for(i=0;i<G.vexnum;i++){
getchar();
scanf("%c",&G.vertices[i].data);
G.vertices[i].firstarc=NULL;
}
printf("请输入顶点序号:\n");
for(i=0;i<G.arcnum;i++){
scanf("%d,%d",&s,&d);
p=(ArcNode*)malloc(sizeof(ArcNode));
q=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=d;
q->adjvex=s;
p->nextarc=G.vertices[s].firstarc;
G.vertices[s].firstarc=p;
q->nextarc=G.vertices[d].firstarc;
G.vertices[d].firstarc=q;
}
}
int CreateDG2(ALGraph &G){
int i,s,d;
ArcNode *p;
printf("请输入顶点数,边数:\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("请输入顶点值(字符)\n");
for(i=0;i<G.vexnum;i++){
getchar();
scanf("%c",&G.vertices[i].data);
G.vertices[i].firstarc=NULL;
}
printf("请输入顶点序号:\n");
for(i=0;i<G.arcnum;i++){
scanf("%d,%d",&s,&d);
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=d;
p->nextarc=G.vertices[s].firstarc;
G.vertices[s].firstarc=p;
}
}
int CreateDN2(ALGraph &G){
int i,s,d,IncInfo;
ArcNode *p;
printf("请输入顶点数,边数:\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("请输入顶点值(字符)");
for(i=0;i<G.vexnum;i++){
getchar();
scanf("%c",&G.vertices[i].data);
G.vertices[i].firstarc=NULL;
}
printf("请输入顶点序号:\n");
for(i=0;i<G.arcnum;i++){
scanf("%d,%d",&s,&d);
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=d;
p->nextarc=G.vertices[s].firstarc;
G.vertices[s].firstarc=p;
printf("此边是否含有其他信息?(0.是 1. 否)");
scanf("%d",&IncInfo);
if(IncInfo){
printf("请输入相关信息\n");
scanf("%s",p->info);
}
}
}
int CreateUDN2(ALGraph &G){
int i,s,d,IncInfo;
ArcNode *p,*q;
printf("请输入顶点数,边数:\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("请输入顶点值(字符)\n");
for(i=0;i<G.vexnum;i++){
getchar();
scanf("%c",&G.vertices[i].data);
G.vertices[i].firstarc=NULL;
}
printf("请输入顶点序号:\n");
for(i=0;i<G.arcnum;i++){
scanf("%d,%d",&s,&d);
p=(ArcNode*)malloc(sizeof(ArcNode));
q=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=d;
q->adjvex=s;
p->nextarc=G.vertices[s].firstarc;
G.vertices[s].firstarc=p;
q->nextarc=G.vertices[d].firstarc;
G.vertices[d].firstarc=q;
printf("此边是否含有其他信息?(0.是 1. 否)");
scanf("%d",&IncInfo);
if(IncInfo){
printf("请输入相关信息\n");
scanf("%s",p->info);
}
}
}
int CreateGraph2(ALGraph &G) {
printf("(建立在邻接表存储结构下的遍历)请输入你要创建的图的类型:\n1、有向图;2、有向网;3、无向图;4、无向网\n");
scanf("%d",&G.kind);
switch(G.kind) {
case 1: CreateDG2(G);break;
case 2: CreateDN2(G);break;
case 3: CreateUDG2(G);break;
case 4: CreateUDN2(G);break;
default:return ERROR;
}
}
int(*VisitFunc)(int v,ALGraph G);
bool visited[MAX_VERTEX_NUM];
//DFS
int DFS(ALGraph G,int v){
ArcNode *p;
visited[v]=true;
(*VisitFunc)(v,G);
for(p=G.vertices[v].firstarc;p;p=p->nextarc){
if(!visited[p->adjvex])
DFS(G,p->adjvex);
}
}
//DFSraverse
int DFSTraverse(ALGraph G,int(*Visit)(int v,ALGraph G)){
int v;
VisitFunc=Visit;
for(v=0;v<G.vexnum;++v)
visited[v]=false;
for(v=0;v<G.vexnum;++v){
if(!visited[v])
DFS(G,v);
}
}
//v函数
int Printelem(int v,ALGraph G){
printf("%c ",G.vertices[v].data);
}
//prim算法
typedef struct{
char adjvex;
int lowcost;
}closedge[MAX_VERTEX_NUM];
int mininum(closedge Z,MGraph M)
{
int i=0,k,j,min;
while(!Z[i].lowcost)
i++;
min=Z[i].lowcost;
k=i;
for(j=i+1;j<M.vexnum;j++)
{if(Z[j].lowcost>0&&Z[j].lowcost<min)
{min=Z[j].lowcost;
k=j;
}
}
return k;
}
int MiniSpanTree_PRIM(MGraph G,char u){
int k,j,i;
closedge T;
k=LocateVex(G,u);
for(j=0;j<G.vexnum;++j){
if(j!=k)
{T[j].adjvex=u;
T[j].lowcost=G.arcs[k][j].adj;
}
}
T[k].lowcost=0;
for(i=1;i<G.vexnum;++i){
k=mininum(T,G);
printf("(%d,%d) ",T[k].adjvex,G.vexs[k]);
T[k].lowcost=0;
for(j=0;j<G.vexnum;++j){
if(G.arcs[k][j].adj<T[j].lowcost)
{
T[j].adjvex=G.vexs[k];
T[j].lowcost=G.arcs[k][j].adj;
}
}
}
}
int main(){
ALGraph G;
MGraph M;
char u;
CreateGraph2(G);
DFSTraverse(G,Printelem);
CreateUDN(M);
printf("请选择最小生成树的起点:\n");
scanf("%c",&u);
printf("\n");
MiniSpanTree_PRIM(M,u);
return 0;
}