|
void InputGraph(int a[100][100])//用户将图输入
{
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[i][j]=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][j]||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[i][j])
{
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[i][j]==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[i][j]=a[j][i]=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[i][j])
printf("(%d,%d) ",i,j);
printf("If you want to amend,please input (i,j).");
printf("And if the graph have edge from vex[i] to vex[j],");
printf("it would delete the edge.");
printf("Else,it would add an edge from vex[i] to vex[j].\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[i][j]=!a[i][j];a[j][i]=!a[j][i];
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[100][100],*m;//设个辅助矩阵输入时用
int i,j;
int FE[100];//纪录依附于各点的第一条边是否已经储存。
struct Edge *p[100];//设置追踪指针
InputGraph(matrix);
for(i=1;i<=G.VexNum;i++)
FE[i]=0;
for(i=0;i<=99;i++)//把图的各顶点的“第一条邻接边”指针初始化为空指针
{
G.GVex[i].data=i;
G.GVex[i].FirstEdge=NULL;
}
for(i=1;i<=G.VexNum;i++)//储存图
for(j=i;j<=G.VexNum;j++)
if(matrix[i][j])
{
if(!FE[i])//如果该点的“第一条边”还没接储存,则此边为该点的“第一条邻接边”
{
G.GVex[i].FirstEdge=(struct Edge*)malloc(sizeof(struct Edge));
p[i]=G.GVex[i].FirstEdge;
FE[i]=1;
}//if(!FE[i])
else if(p[i]->ivex==i)
{
p[i]->ilink=(struct Edge*)malloc(sizeof(struct Edge));
p[i]=p[i]->ilink;
}//else if(p[i]->ivex==i)
else if(p[i]->jvex==i)
{
p[i]->jlink=(struct Edge*)malloc(sizeof(struct Edge));
p[i]=p[i]->jlink;
}//else if(p[i]->jvex==i)
p[i]->ivex=i;p[i]->jvex=j;
p[i]->ilink=NULL;p[i]->jlink=NULL;
if(!FE[j])
{
G.GVex[j].FirstEdge=p[i];
p[j]=p[i];
}//if(!FE[j])
else if(p[j]->ivex==j)
{
p[j]->ilink=p[i];
p[j]=p[i];
}//else if(p[j]->ivex==j)
else if(p[j]->jvex==j)
{
p[j]->jlink=p[i];
p[j]=p[i];
}//else if(p[j]->jvex==j)
}//if(matrix[i][j])
}//用多重邻接表存储图
[[i] 本帖最后由 powerwind 于 2007-8-13 20:05 编辑 [/i]] |
|