数据结构的一道作业题目:
建立图的邻接矩阵,并判断两点是否相邻。
程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#define M 20
/*定义图*/
typedef struct
{
int V[M];
int R[M][M];
int vexnum; /*图的顶点数*/
}Graph;
/*创建图*/
void creatgraph(Graph *g,int n)
{
int i,j,r1,r2;
g->vexnum=n;
/*顶点用i表示*/
for(i=1;i<=n;i++)
{
g->V=i;
}
/*初始化R*/
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
g->R[j]=0;
}
/*输入R*/
printf("Please input R(0,0 END):\n");
scanf("%d,%d",&r1,&r2);
while(r1!=0&&r2!=0)
{
g->R[r1][r2]=1;
g->R[r2][r1]=1;
scanf("%d,%d",&r1,&r2);
}
}
/*打印图的邻接矩阵*/
void printgraph(Graph *g)
{
int i,j;
for(i=1;i<=g->vexnum;i++)
{
for(j=1;j<=g->vexnum;j++)
{
printf("%2d ",g->R[j]);
}
printf("\n");
}
}
/*判断图的两个顶点是否相邻*/
void judgegraph(Graph *g,int i, int j)
{
printf("Please input two vertexs:\n");
scanf("%d","%d",&i,&j);
if(g->R[j]==1)
printf("The two vertexs are near.\n");
else if(g->R[j]==0)
printf("The two vertexs aren't near.\n");
else
printf("FLAUSE.\n");
}
/*主程序*/
void main()
{
int n,i,j;
Graph *g=(Graph *)malloc(sizeof(Graph));
printf("Create the linjiejuzhen of graph and judge two vertexs are near or not.\n");
printf("Please input the number of vertex:\n");
scanf("%d",&n);
creatgraph(g,n);
printf("This is the linjiejuzhen of graph:\n");
printgraph(g);
printf("\n");
judgegraph(g,i,j);
}
运行结果如下:
本程序输入的图例子是下面这个图:
结果运行可以了,但程序编译信息仍然有两处警告显示如下:
警告 c:\progra~1\tcwin\tu19.c 80: 可能在'i'定义以前使用了它 在函数
警告 c:\progra~1\tcwin\tu19.c 80: 可能在'j'定义以前使用了它 在函数
这个……??