采用邻接矩阵创建一个有向网N
分析:图的创建主要利用输入的各个顶点,并存储到一个向量(一维数组)中,然后通过输入两个顶点及权重创建弧,利用二维数组表示,因此,利用邻接矩阵创建图的存储需要两个数组:一个一维数组和一个二维数组。
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
typedef char VertexType[4];
typedef char InfoPtr;
typedef int VRType;
#define INFINITY 10000//定义一个无限大的值
#define MAXSIZE 100//最大顶点个数
typedef enum {DG,DN,UG,UN}GraphKind;
//图的类型:有向图,有向网,无向图和无向网
typedef struct
{
VRType adj;//对于无权图,用1表示相邻,0表示不相邻;对于带权图,存储权值
InfoPtr *info;//与弧或边的相关信息
}ArcNode,AdjMatrix[MAXSIZE][MAXSIZE];
typedef struct//图的类型定义
{
VertexType vex[MAXSIZE];//用于存储顶点
AdjMatrix arc;//邻接矩阵,存储边或弧的信息
int vexnum,arcnum;//顶点数和边的数目
GraphKind kind;//图的类型
}MGraph;
int LocateVertex(MGraph N,VertexType v)
//在顶点向量中查找顶点v,找到返回在向量的序号,否则返回-1
{
int i;
for(i=0;i<N.vexnum ;++i)
if(strcmp(N.vex [i],v)==0)
return i;
return -1;
}
void CreateGraph(MGraph *N)//采用邻接矩阵表示法创建有向网N
{
int i,j,k,w,InfoFlag,len;
char s[MAXSIZE];
VertexType v1,v2;
printf("请输入有向网N的顶点数,弧数,弧的信息(是:1,否:0):");
scanf("%d,%d,%d",&(*N).vexnum ,&(*N).arcnum ,&InfoFlag);
printf("请输入%d个顶点的值(<%d个字符):\n",N->vexnum ,MAXSIZE);
for(i=0;i<N->vexnum;i++)//创建一个数组,用于保存网的各个顶点
scanf("%s",N->vex [i]);
for(i=0;i<N->vexnum ;i++)//初始化邻接矩阵
for(j=0;j<N->vexnum ;j++)
{
N->arc [i][j].adj =INFINITY;
N->arc [i][j].info =NULL;//弧的信息初始化为空
}
printf("请输入%d条弧的弧尾 弧头 权值(以空格作为间隔):\n",N->arcnum );
for(k=0;k<N->arcnum ;k++)
{
scanf("%s%s%d",v1,v2,&w);//输入两个顶点和弧的权值
i=LocateVertex(*N,v1);
j=LocateVertex(*N,v2);
N->arc [i][j].adj =w;
if(InfoFlag)//如果弧包含其他信息
{
printf("请输入弧的相关信息:");
gets(s);
len=strlen(s);
if(len)
{
N->arc [i][j].info =(char*)malloc((len+1)*sizeof(char));//有向
strcpy(N->arc [i][j].info ,s);
}
}
}
N->kind =DN;//图的类型为有向网
}
void DestroyGraph(MGraph *N)//销毁网
{
int i,j;
for(i=0;i<N->vexnum ;i++)
for(j=0;j<N->vexnum ;j++)
if(N->arc [i][j].adj !=INFINITY)//如果存在弧
if(N->arc [i][j].info !=NULL)
//如果弧有相关信息,释放该信息所占用空间
{
free(N->arc [i][j].info );
N->arc [i][j].info =NULL;
}
N->vexnum =0;//将网的顶点数置为0
N->arcnum =0;//将网的弧的数目置为0
}
void DisplayGraph(MGraph N)//输出邻接矩阵存储表示的图N
{
int i,j;
printf("有向网具有%d个顶点%d条弧,顶点依次是:",N.vexnum ,N.arcnum );
for(i=0;i<N.vexnum ;++i)
printf("%s",N.vex [i]);//输出网的顶点
printf("\n有向网N的:\n");//输出网N的弧
printf("序号i=");
for(i=0;i<N.vexnum ;i++)
printf("%8d",i);
printf("\n");
for(i=0;i<N.vexnum ;i++)
{
printf("%8d",i);
for(j=0;j<N.vexnum ;j++)
printf("%8d",N.arc [i][j].adj );
printf("\n");
}
}
void CreateGraph(MGraph *N);
int LocateVertex(MGraph N,VertexType v);
void DestroyGraph(MGraph *N);
void DisplayGraph(MGraph N);
void main()
{
MGraph N;
printf("创建一个网:\n");
CreateGraph(&N);
printf("输出网的顶点和弧:\n");
DisplayGraph(N);
printf("销毁网:\n");
DestroyGraph(&N);
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/weichanjuan3/article/details/47362137