欢迎来到天天文库
浏览记录
ID:47478904
大小:1.35 MB
页数:3页
时间:2020-01-11
《将一个无向图的邻接表转换为邻接矩阵》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#definemax20#definedigit1#definezero0typedefstruct{intnum;chardata;}Vertex;typedefstruct{intn;//顶点数inte;//弧数Vertexvexs[max];intedges[max][max];}MGraph;typedefstructnode{intadjvex;node*nextarc;charinfo;}ARCNODE;//邻接表的结点结构typedefstruct{c
2、harvexdata;ARCNODE*firstarc;}VEXNODE;//邻接表的表头结点typedefstruct{intvexnum,arcnum;//顶点数、弧数VEXNODEve[max];}ALGraph;//邻接表类型ALGraph*Creat_alg(){//创建邻接表ALGraph*alg;inti,n,e,b,a;charch;ARCNODE*AR;alg=(ALGraph*)malloc(sizeof(ALGraph));printf("输入顶点数:");scanf("%d",&n);printf("输入弧数:
3、");scanf("%d",&e);alg->vexnum=n;alg->arcnum=e;printf("输入顶点信息:");for(i=0;ive[i].vexdata=ch;alg->ve[i].firstarc=NULL;}printf("输入弧的信息(弧的两端点):");for(i=0;iadjvex=b;AR->info=
4、alg->ve[b].vexdata;AR->nextarc=alg->ve[a].firstarc;alg->ve[a].firstarc=AR;AR=(ARCNODE*)malloc(sizeof(ARCNODE));AR->adjvex=a;AR->info=alg->ve[a].vexdata;AR->nextarc=alg->ve[b].firstarc;alg->ve[b].firstarc=AR;}returnalg;}voidALGout(ALGraph*alg){//邻接表输出inti,n1;ARCNODE*p;VE
5、XNODE*q;n1=alg->vexnum;for(i=0;ive[i];printf("%c",q->vexdata);p=q->firstarc;while(p!=NULL){printf("─→");printf("%c",p->info);p=p->nextarc;}printf("");}}MGraph*ALG_change_MG(ALGraph*alg){//将邻接表转换为邻接矩阵MGraph*mg;inti,n1;mg=(MGraph*)malloc(sizeof(MGraph))
6、;mg->n=alg->vexnum;mg->e=alg->arcnum;n1=mg->n;for(i=0;ivexs[i].num=i;mg->vexs[i].data=alg->ve[i].vexdata;}ARCNODE*p;for(i=0;ive[i].firstarc;while(p!=NULL){mg->edges[i][p->adjvex]=1;mg->edges[p->adjvex][i]=1;p=p->nextarc;}}returnmg;}voidMGout
7、(MGraph*mg){//输出邻接矩阵inti,j,k;k=mg->n;for(i=0;iedges[i][j]==1)printf("%-5d",digit);elseprintf("%-5d",zero);}printf("");}}voidmain(){MGraph*mg;ALGraph*alg;printf("建立无向图的邻接表:");alg=Creat_alg();printf("邻接表输出:");ALGout(alg);mg=ALG_change
8、_MG(alg);printf("邻接矩阵输出:");MGout(mg);}
此文档下载收益归作者所有