资源描述:
《c++实现邻接矩阵_数据结构》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include"stdio.h"#definemax_num20typedefenum{DG,DN,UDG,UDN}Graphkind;typedefstructArcCell{intadj;char*info;}ArcCell,AdjMatrix[max_num][max_num];typedefstruct{charvexs[max_num];//顶点向量AdjMatrixarcs;//定义邻接矩阵intvexnum;//定义图当前的定点数intarcnum;//定义图当前的弧数Graphkindkind;//图的种类标志}MGraph;voidCreateDG_DN(MGr
2、aph*G);voidCreateUDN_UDG(MGraph*G);voidCreateGraph(MGraph*G);intLocateVex(MGraph*G,charc);voidDisplayGraph(MGraph*G);voidmain(){MGraphG;CreateGraph(&G);DisplayGraph(&G);}voidCreateGraph(MGraph*G){printf("输入图的类型(DG,DN,UDG,UDN对应选择0,1,2,3):");scanf("%d",&G->kind);switch(G->kind){caseDG:CreateDG_D
3、N(G);break;caseDN:CreateDG_DN(G);break;caseUDG:CreateUDN_UDG(G);break;caseUDN:CreateUDN_UDG(G);break;default:break;}}voidCreateDG_DN(MGraph*G){inti,j,k,w;charv[2],c[10];if(G->kind==0)printf("CreateDG(有向图)");elseprintf("CreateDN(无向图)");printf("输入顶点数和边数(弧数):");scanf("%d%d",&G->vexnum,&G->a
4、rcnum);printf("顶点数为%d,边数(弧数)为%d",G->vexnum,G->arcnum);printf("输入每个顶点的数据:");scanf("%s",c);//输入顶点信息for(i=0;ivexnum;i++){G->vexs[i]=c[i];}for(i=0;ivexnum;i++)for(j=0;jvexnum;j++){G->arcs[i][j].adj=0;G->arcs[i][j].info=NULL;//可以自己定义赋值}//初始化矩阵for(k=0;karcnum;k++){printf("输入邻接第%d
5、条边(弧)两顶点:",k+1);scanf("%s",v);//printf("输入边权值:");//scanf("%d",&w);i=LocateVex(G,v[0]);j=LocateVex(G,v[1]);G->arcs[i][j].adj=1;if(G->kind==1)G->arcs[j][i]=G->arcs[i][j];//无向图是对角矩阵}}voidCreateUDN_UDG(MGraph*G){inti,j,k,w;charv[2],c[10];if(G->kind==3)printf("CreateUDN(无向网)");elseprintf("CreateU
6、DG(有向网)");printf("输入顶点数和边数(弧数):");scanf("%d%d",&G->vexnum,&G->arcnum);printf("顶点数为%d,边数(弧数)数为%d",G->vexnum,G->arcnum);printf("输入每个顶点的数据:");scanf("%s",c);//输入顶点信息for(i=0;ivexnum;i++){G->vexs[i]=c[i];}for(i=0;ivexnum;i++)for(j=0;jvexnum;j++){G->arcs[i][j].adj=0;G->arcs[i][j].
7、info=NULL;//可以自己定义赋值}//初始化矩阵for(k=0;karcnum;k++){printf("输入邻接第%d条边(弧)两顶点:",k+1);scanf("%s",v);printf("输入边(弧)权值:");scanf("%d",&w);i=LocateVex(G,v[0]);j=LocateVex(G,v[1]);G->arcs[i][j].adj=w;if(G->kind==3)G->arcs[j][i]=G->arcs[i][j];/