数据结构图的遍历
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include "stdlib.h"
#include "stdio.h"
#include "malloc.h" #define INFINITY 32767
#define MAX_VERTEX_NUM 20 typedef enum{ FALSE,TRUE } visited_hc; typedef enum{ DG,DN,UDG ,UDN } graphkind_hc;
typedef struct arccell_hc
{int adj;
int * info;
} arccell_hc,adjmatrix_hc[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{char vexs[MAX_VERTEX_NUM]; adjmatrix_hc arcs;
int vexnum,arcnum; graphkind_hc kind;
} mgraph_hc;
typedef struct arcnode_hc {int adjvex;
struct arcnode_hc *nextarc; int * info;
} arcnode_hc;
typedef struct vnode_hc
{char data; arcnode_hc * firstarc;
} vnode_hc,adjlist_hc[MAX_VERTEX_NUM];
typedef struct
{ adjlist_hc vertices; int vexnum,arcnum; graphkind_hc kind;
} algraph_hc;
int locatevex_hc(mgraph_hc *g,char v) {int i,k =0;
for(i=0;i
return(k);}
mgraph_hc*createudg_hc()
{ mgraph_hc* g;
char v1,v2;
int i,j,incinfo;
g=(mgraph_hc* )malloc( sizeof(mgraph_hc)); g->kind=UDG;
printf( "请输入图顶点数、边数及该边相关信息:");
scanf("%d %d %d" ,& g->vexnum, & g->arcnum,& incinfo); printf( "请输入顶点信息:");flushall();
for(i=0;i
for(i=0;i
flushall();scanf( "%c%c" ,& v1,& v2);
while(v1 !='#'&& v2!='#')
{ i=locatevex_hc(g,v1);j =locatevex_hc(g,v2); g->arcs[i][j] .adj=1;
if (incinfo)g ->arcs[i][j] .info =& incinfo;
g->arcs[j][i] .adj=g->arcs[i][j] .adj; g->arcs[j][i] .info =g->arcs[i][j] .info; flushall();scanf( "%c%c" ,& v1,& v2); } return(g);}
visited_hc vis[MAX_VERTEX_NUM];
int firstadjvex_hc(mgraph_hc *g,int v)
{int i,k =-1;
for(i=0;i
if (g->arcs[v][i] .adj==1){k=i;i=g->vexnum;}
return(k);}
int nextadjvex_hc(mgraph_hc * g,int v,int w)
{int i,k =-1;
for(i=0;i
if (g->arcs[v][i] . adj== 1&& i>w){k=i;i=g->vexnum;} return(k);}
void dfs_hc(mgraph_hc*g,int v)
{int w;
vis[v] =TRUE; printf( "%c" ,g->vexs[v]);
for(w=firstadjvex_hc(g,v);w >=0;w=nextadjvex_hc(g,v,w)) if (!
vis[w])dfs_hc(g,w); }
void dfstraverse_hc(mgraph_hc* g)
{int v,i; char f;
for(v=0;v
printf( "输入遍历开始顶点:" );flushall();scanf( "%c" ,& f);
i=locatevex_hc(g,f);printf( "深度遍历结果为:");
for(v=i;v
for(v=0;v
{int i,k =0; for(i=0;i
char createlist_hc(algraph_hc*a,arcnode_hc*firstl, char v)
{ arcnode_hc* nextl;
if (v!= '\n')
{ nextl=(arcnode_hc* )malloc( sizeof(arcnode_hc));
nextl->adjvex=locatevexal_hc(a,v); nextl->nextarc=NULL;nextl ->info =firstl ->info; firstl ->nextarc=nextl;
scanf("%c" ,& v);v=createlist_hc(a,nextl,v); } return(v);}
algraph_hc*createaludg_hc()
{ algraph_hc* a;int i,incinfo; char v;