用多重淋接表储存图,我这样的算法有问题吗?
void InputGraph(int a)//用户将图输入{
int i,j,k;
char YN;
printf("The quantity of vex is:");
scanf("%d",&G.VexNum);getchar();
for(i=1;i<=99;i++)
for(j=1;j<=99;j++)
a=0;
printf("The number of edge is:");
scanf("%d",&G.EdgeNum);getchar();
for(k=1;k<=G.EdgeNum;k++)
{
printf("edge %d(i<j):i,j=",k);
scanf("%d,%d",&i,&j);getchar();
while(i>=j||a||i>G.VexNum||j>G.VexNum)
{
if(i>=j)
{
printf("i should less than j\n");
printf("edge %d(i<j):i,j=",k);
scanf("%d,%d",&i,&j);getchar();
}//if(i>j)
if(a)
{
printf("This edge has been input.Please input another edge.\n");
printf("edge %d(i<j):i,j=",k);
scanf("%d,%d",&i,&j);getchar();
}//if(a==1)
if(i>G.VexNum||j>G.VexNum)
{
printf("i and j should be less than vex number.Please input again!\n");
printf("edge %d(i<j):i,j=",k);
scanf("%d,%d",&i,&j);getchar();
}//if(i>G.VexNum||j>G.VexNum)
}//while(input error)
//以上while是出错处理
a=a=1;
}//for(k)
printf("Input finished.Do you want to amend?(Y/N):");//询问是否需要修改图
scanf("%c",&YN);getchar();
while(YN!='Y'&&YN!='y'&&YN!='N'&&YN!='n')
{
printf("Choose error!Please input the right choose(Y/N):");
scanf("%c",&YN);getchar();
}
while(YN=='Y'||YN=='y')
{
printf("The edges of graph is:\n");
for(i=1;i<=99;i++)
for(j=i;j<=99;j++)
if(a)
printf("(%d,%d) ",i,j);
printf("If you want to amend,please input (i,j).");
printf("And if the graph have edge from vex to vex,");
printf("it would delete the edge.");
printf("Else,it would add an edge from vex to vex.\n");
printf("(i<j):i,j=");
scanf("%d,%d",&i,&j);getchar();
while(i>=j||i>G.VexNum||j>G.VexNum)
{
if(i>=j)
{
printf("i should less than j\n");
printf("(i<j):i,j=");
scanf("%d,%d",&i,&j);getchar();
}//if(i>j)
if(i>G.VexNum||j>G.VexNum)
{
printf("i and j should be less than vex number.Please input again!\n");
printf("(i<j):i,j=");
scanf("%d,%d",&i,&j);getchar();
}//if(i>G.VexNum||j>G.VexNum)
}//while(input error)
a=!a;a=!a;
printf("Amend success.");
printf("Do you want to go on amend?(Y/N)");
scanf("%c",&YN);getchar();
while(YN!='Y'&&YN!='y'&&YN!='N'&&YN!='n')
{
printf("Choose error!Please input the right choose(Y/N):");
scanf("%c",&YN);getchar();
}
}//while(需要修改)
}//把图输入
void StoreGraph()//用多重邻接表储存图
{
int matrix,*m;//设个辅助矩阵输入时用
int i,j;
int FE;//纪录依附于各点的第一条边是否已经储存。
struct Edge *p;//设置追踪指针
InputGraph(matrix);
for(i=1;i<=G.VexNum;i++)
FE=0;
for(i=0;i<=99;i++)//把图的各顶点的“第一条邻接边”指针初始化为空指针
{
G.GVex.data=i;
G.GVex.FirstEdge=NULL;
}
for(i=1;i<=G.VexNum;i++)//储存图
for(j=i;j<=G.VexNum;j++)
if(matrix)
{
if(!FE)//如果该点的“第一条边”还没接储存,则此边为该点的“第一条邻接边”
{
G.GVex.FirstEdge=(struct Edge*)malloc(sizeof(struct Edge));
p=G.GVex.FirstEdge;
FE=1;
}//if(!FE)
else if(p->ivex==i)
{
p->ilink=(struct Edge*)malloc(sizeof(struct Edge));
p=p->ilink;
}//else if(p->ivex==i)
else if(p->jvex==i)
{
p->jlink=(struct Edge*)malloc(sizeof(struct Edge));
p=p->jlink;
}//else if(p->jvex==i)
p->ivex=i;p->jvex=j;
p->ilink=NULL;p->jlink=NULL;
if(!FE)
{
G.GVex.FirstEdge=p;
p=p;
}//if(!FE)
else if(p->ivex==j)
{
p->ilink=p;
p=p;
}//else if(p->ivex==j)
else if(p->jvex==j)
{
p->jlink=p;
p=p;
}//else if(p->jvex==j)
}//if(matrix)
}//用多重邻接表存储图
[ 本帖最后由 powerwind 于 2007-8-13 20:05 编辑 ]
页:
[1]