欢迎来到天天文库
浏览记录
ID:51701672
大小:31.58 KB
页数:3页
时间:2020-03-15
《图的头文件(C语言).docx》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#includetypedefintAdjType;typedefstruct{charinfo;intmark;}VexType;typedefVexType*Vertex;typedefAdjType*Adj;typedefstruct{intn;Vertexvexs;Adj*arcs;}GraMatrix;typedefGraMatrix*Graph;GraphcreateGraph(intn){Graphg;inti,j;charx;g=(Graph)malloc(sizeof(GraMatrix));g->n=n;g->
2、vexs=(Vertex)malloc(sizeof(VexType)*n);printf("输入顶点");for(i=0;ivexs[i].info);g->vexs[i].mark=0;}g->arcs=(Adj*)malloc(sizeof(Adj)*n);for(i=0;iarcs[i]=(Adj)malloc(sizeof(AdjType)*n);printf("输入矩阵第%d行",i);for(j=0;jarcs[i][j]);}
3、returng;}intisNullGraph(Graphg){return(g->n==0);}VertexfirstVertex(Graphg){return(g->vexs);}VertexnextVertex(Graphg,Vertexvi){if(vi-g->vexs==g->n-1)returnNULL;elsereturn(vi+1);}VertexsearchVertex(Graphg,charvalue){inti;for(i=0;in;i++){if(g->vexs->info==value)return(g->vexs);g->vexs++;}}Vert
4、exfirstAdjacent(Graphg,Vertexv){inti,j;i=v-g->vexs;for(j=0;jn;j++)if(g->arcs[i][j]!=0&&g->arcs[i][j]!=10000)return(g->vexs+j);}VertexnextAdjacent(Graphg,Vertexvi,Vertexvj){inti,j,k;i=vi-g->vexs;j=vj-g->vexs;for(k=j+1;kn;k++)if(g->arcs[i][k]!=0&&g->arcs[i][k]!=10000)return(g->vexs+k);ret
5、urnNULL;}voiddfs(Graphg,Vertexv){Vertexv1;v->mark=1;printf("%c",v->info);for(v1=firstAdjacent(g,v);v1!=NULL;v1=nextAdjacent(g,v,v1))if(v1->mark==0)dfs(g,v1);}voiddft(Graphg){Vertexv;intx;for(v=firstVertex(g);v!=NULL;v=nextVertex(g,v)){x=v->mark;if(v->mark==0)dfs(g,v);}printf("");}
此文档下载收益归作者所有