2 sunqiang20111 sunqiang20111 于 2014.11.11 17:13 提问

简单的图实现,编译成功运行不了,求指导

包括4个部分代码

[ 主函数: ]1

#include
#include
typedef char DataType;
#define MaxSize 100
#define MaxVertices 10
#define MaxEdges 100
#define MaxWeight 10000

#include"AdjMGragh.h"
#include"AdjMGraghCreate.h"

int main()
{
AdjMGragh g1;
DataType a[]={'A','B','C','D','E'};
RowColWeight rcw[]={{0,1,10},{0,4,20},{1,3,30},{2,1,40},{3,2,50}};
int n=5,e=5;
int i,j;

CreateGragh(&g1,a,n,rcw,e);
DeleteEdge(&g1,0,4) ;
DeleteVerten(&g1,3);

printf("节点集合为:");
for(i=0;i<g1.Vertices.size;i++) 
    printf("%c   ",g1.Vertices.list[i]);
    printf("\n");
printf("权值集合为:\n");
for(i=0;i<g1.Vertices.size;i++)
{
    for(j=0;j<g1.Vertices.size;j++)
    printf("%5d  ",g1.edge[i][j]);

    printf("\n");
}

}

头文件:"AdjMGragh.h";AdjMGraghCreate.h;SeqList;

** AdjMGragh.h: **
#include"SeqList.h"
#include
#include
#define MaxSize 100
#define MaxVertices 10
#define MaxEdges 100
#define MaxWeight 10000
typedef struct
{
SeqList Vertices;
int edge[MaxVertices][MaxVertices];
int numOfEdges;
}AdjMGragh;

void Initiate(AdjMGragh *G,int n)
{
int i,j;

for(i=0;i for(j=0;j {
if(i==j) G->edge[i][j]=0;
else G->edge[i][j]=MaxWeight;
}
G->numOfEdges=0;
ListInitiate(&G->Vertices);
}

void InsertVertex(AdjMGragh *G,DataType vertex)
{
ListInsert(&G->Vertices,G->Vertices.size,vertex);

}

void InsertEdge(AdjMGragh *G,int v1,int v2,int weight)
{
if(v1G->Vertices.size||v2G->Vertices.size)
{
printf("参数v1或v2越界出错!\n");
exit(1);
}

G->edge[v1][v2]=weight;
G->numOfEdges++;

}

void DeleteEdge(AdjMGragh *G,int v1,int v2)
{
if(v1G->Vertices.size||v2G->Vertices.size)
{
printf("参数v1或v2越界出错!\n");
exit(1);
}
if(G->edge[v1][v2]==MaxWeight||v1==v2)
{
printf("该连接不存在!\n");
exit(0);
}
G->edge[v1][v2]=MaxWeight;
G->numOfEdges--;
}

void DeleteVerten(AdjMGragh *G,int v)
{
int n=ListLength(G->Vertices),i,j;
DataType x;

for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    if(((i=v)||(j=v))&&G->edge[i][j]>0&&G->edge[i][j]<MaxWeight)
    G->numOfEdges--;
for(i=v;i<n;i++)
for(j=0;j<n;j++)
{
    G->edge[i][j]=G->edge[i+1][j];
} 

for(i=0;i<n;i++)
for(j=v;j<n;j++)
{
    G->edge[i][j]=G->edge[i][j+1];
}

ListDelete(&G->Vertices,v,&x);

}

int GetFirstVex(AdjMGragh G,int v)
{
int col;
if(vG.Vertices.size)
{
printf("参数v1越界出错!\n");
exit(1);
}

for(col=0;col<G.Vertices.size;col++)
if(G.edge[v][col]>0&&G.edge[v][col]<MaxWeight) return col;
return col;
return -1;

}

int GetNextVertices(AdjMGragh G,int v1,int v2)
{
int col;
if(v1MaxWeight||v2MaxWeight)
{
printf("参数v1或v2越界!\n");
exit(1);
}

for(col=v1+v2;col<G.Vertices.size;col++)
{
    if(G.edge[v1][col]>0&&G.edge[v1][col]<MaxWeight) return col;
    return -1;
}

}

** AdjMGraghCreate.h: **
typedef struct
{
int row;
int col;
int weight;
}RowColWeight;

void CreateGragh(AdjMGragh *G,DataType V[],int n,RowColWeight E[],int e)
{
int i,k;
Initiate(G,n);

for(i=0;i<n;i++)
{
    InsertVertex(G,V[i]);
}
for(k=0;k<e;k++)
InsertEdge(G,E[k].row,E[k].col,E[k].weight);

}

** SeqList.h: **
#include
#define MaxSize 100
typedef struct
{
DataType list[MaxSize];
int size;

}SeqList;

void ListInitiate(SeqList *L)
{
L->size=0;
}

int ListLength(SeqList L)
{
return L.size;
}

int ListInsert(SeqList *L,int i,DataType x)
{
int j;
if(L->size>=MaxSize)
{
printf("顺序表已无法插入!\n");
return 0;
}
else if(iL->size)
{
printf("参数不合法!\n");
return 0;
}
else
{
for(j=L->size;j>i;j--) L->list[j]=L->list[j-1];
L->list[i]=x;
L->size++;
return 1;
}
}

int ListDelete(SeqList *L,int i,DataType *x)
{
int j;
if(L->size<=0)
{
printf("顺序表已无法删除!\n");
return 0;
}
else if(iL->size)
{
printf("参数不合法!\n");
return 0;
}
else
{
*x=L->list[i];
for(j=L->size;j>i;j--) L->list[j-1]=L->list[j];
L->size--;
return 1;
}
}

int ListGet(SeqList L,int i,DataType *x)
{
if(iL.size-1)
{
printf("参数不合法!\n");
return 0;
}
else
{
*x=L.list[i];
return 1;
}
}
DataType ListElm(SeqList *L,int i)
{
return L->list[i];
}

Csdn user default icon
上传中...
上传图片
插入图片