|
#define NULL 0
#include<stdio.h>
#include<malloc.h>
typedef struct/*图的结构体*/
{
int a[21];/*存放顶点的一唯数组*/
int b[21][21];/*存放关系的二唯数组*/
int vexnum;/*顶点数*/
int arcnum;/*弧数*/
}graph;
graph *initializtion_graph(void);/*函数声明,图的初始化*/
graph * creategraph(graph *s);/*函数声明,图的建立*/
void printgraph(graph *s);/*函数声明,图的输出*/
main()/*主函数*/
{
graph *g;
g=initializtion_graph();/*初始化*/
creategraph(g);/*建立图*/
printgraph(g);/*输出图*/
printf("\n");
getch();
}
graph *initializtion_graph(void)/*图的初始化*/
{
graph *g;
int i,j;
g=(graph *)malloc(sizeof(graph*));/*分配内存空间*/
for(i=1;i<=20;i++)/*全部置0*/
{
g->a=0;/*初始化*/
}
for(i=1;i<=20;i++)
{
for(j=1;j<=20;j++)
{
g->b[j]=0;/*初始化*/
}
}
g->vexnum=0;
g->arcnum=0;
return(g);/*返回指针*/
}
/*------------------------------------------------------------*/
graph *creategraph(graph *s)/*建立一个图*/
{
int i,j;
printf("please input the number of the node!\n");
scanf("%d",&(s->vexnum));/*输入节点数*/
printf("please input the cost of the node!\n");
for(i=1;i<=(s->vexnum);i++)/*输入具体的节点值*/
{
scanf("%d",&(s->a));
}
printf("please input the arc!\n");
for(i=1;i<=(s->vexnum);i++)/*输入弧的关系,半个距阵来保存*/
{
for(j=1;j<=i;j++)
{
if(i==j)
s->b[j]=0;/*中间的全部为0*/
else
scanf("%d",&(s->b[j]));
}
}
printf("\n");
return(s);
}
/*-----------------------------------------------------------*/
void printgraph(graph *s)/*输出图*/
{
int i,j;
clrscr();
for(i=1;i<=(s->vexnum);i++)
{
printf("%d ",s->a);/*输出节点*/
}
printf("\n");
printf("\n");
for(i=1;i<=(s->vexnum);i++)/*输出节点与节点的关系*/
{
for(j=1;j<=i;j++)
{
printf("%d ",s->b[j]);
if(j==i)
printf("\n");
}
}
printf("the number of the nodeis %d\n",s->vexnum);/*输出节点数*/
for(i=1;i<=(s->vexnum);i++)/*统计弧数*/
{
for(j=1;j<=i;j++)
{
if(s->b[j]==1)
s->arcnum++;
}
}
printf("the number of the arc is %d\n",s->arcnum);/*输出弧*/
printf("\n");
}
/*-------------------------------------------------------------------*/
当我调用printgraph();函数时,为什么s->a的值会被冲掉
输入5
然后输入1,2,3,4,5
……
应该输出1,2,3,4,5
但是输出1,2,3,234,5
怎么会事 |
|