1、V4三、附录:在此贴上调试好的程序。#include#define M 100 typedef struct node char vexM2; int edgeM M ; int n,e; Graph;int visitedM;Graph *Create_Graph() Graph *GA; int i,j,k,w; GA=(Graph*)malloc(sizeof(Graph); printf (请输入矩阵的顶点数和边数(用逗号隔开):n); scanf(%d,%d,&GA-n,&e);请输入矩阵顶点信息: for(i = 0;ivexi0),&vexi1); for (i = 0;i+)
2、for (j = 0;jedgeij = 0; for (k = 0;kvexv0,GA-vexv1); visitedv=1; for(i=0; iedgevi=1 & visitedi=0) dfs(GA, i);void traver(Graph *GA) visitedi=0; if(visitedi=0) dfs(GA, i);void bfs( Graph *GA, int v) int j,k,front=-1,rear=-1; int QM; rear=rear+1; Qrear=v; while (front!=rear) front=front+1;k=Qfront; fo
3、r (j=0; jvexj1); visitedj=1; Qrear=j; void traver1(Graph *GA) for (i=0; i+) if (visitedi=0) bfs(GA, i);typedef struct NODE int adjvex; struct NODE *next; ENode;typedef struct NODE1 char vex2; ENode *first; VexNode;typedef struct FS1 VexNode GLM; int bian,top;FS;FS *CreateGL( ) FS *kk=(FS *)malloc(si
4、zeof(FS); int i,j,k; ENode *s;请输入顶点数和边数(用逗号隔开):kk-top, &bian);请输入顶点信息:top; scanf(,kk-GLi.vex); kk-GLi.first=NULL;请输入边的信息(i,j): for (k=0;bian;k+) n%d,%dj); s =(ENode*)malloc(sizeof(ENode); s-adjvex=j;next=kk-GLi.first;GLi.first =s; return kk;void DFS(FS *kk, int v) ENode *w; int i;%snGLv.vex); w=kk-G
5、Lv.first ; while (w!=NULL) i=w-adjvex; DFS(kk,i); w=w-next;void TRAVER(FS *kk) DFS(kk, i);void BFS(FS *kk, int v) int QM, front=-1,rear=-1; ENode *w; int i, k;=rear) k=Qfront;GLk.first; while(w!=NULL) if( visitedi=0) visitedi=1; Qrear=i;void TRAVER1(FS *kk)i+) visitedi=0; i BFS(kk,i);int main() int
6、i=0; Graph *p; FS *q; while(i=1) /*建立菜单*/ char jz30=1.创建邻接矩阵; char jd30=2.邻接矩阵DFS遍历 char jb30=3.邻接矩阵BFS遍历 char bg30=4.创建邻接表 char bd30=5.邻接表DFS遍历 char bb30=6.邻接表BFS遍历 char tc30=7.退出 char mn30=菜单 int l=strlen(jd); int o=strlen(mn); int m,n; printf( for(m=0;m=(2*l-o)/2;m+) printf( ,mn);=2*l;* %s *n* %s
7、 *n* %s *n* %s *n* %s *n* %s *n* %s *n,jz,jd,jb,bg,bd,bb,tc); /*选择功能*/请输入所需功能序号:%dn); switch(n) case 1: p=Create_Graph();break; case 2: traver(p); case 3: traver1(p); case 4: q=CreateGL(); case 5: TRAVER(q); case 6: TRAVER1(q); case 7: return 0; default:printf(输入功能序号有误! 四、运行结果:在此把运行结果从屏幕上拷下来贴在此五、心得体会:测试数据要注意现实中矩阵是从1开始,而数组里是从0开始。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1