| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 529 人关注过本帖
标题:求助!深度优先遍历
只看楼主 加入收藏
wujiekangyun
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-4-21
收藏
 问题点数:0 回复次数:1 
求助!深度优先遍历
//DFSTraverse.cpp
//This function is to traver ALGraph by DFS Algorithm
# include <iostream.h>
# include <malloc.h>
# include <conio.h>

# define MAX_VERTEX_NUM 20
# define OK 1
typedef int VertexType;
typedef int InfoType;

typedef struct ArcNode //define structure ALGraph
{ int adjvex;
struct ArcNode *nextarc;
InfoType *info;
}ArcNode;

typedef struct VNode
{ VertexType data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];

typedef struct
{ AdjList vertices;
int vexnum,arcnum;
int kind;
}ALGraph;

int CreateDG(ALGraph &G) //CreateDG() sub-fuction
{ int IncInfo,i,j,k,v1,v2,w;
cout<<endl<<"Please input the number of G.vexnum (eg. G.vexnum=4): ";
cin>>G.vexnum; //input the number of vex
cout<<"Please input the number of G.arcnum (eg. G.arcnum=4): ";
cin>>G.arcnum; //input the number of arc
cout<<"Please input the number of IncInfo (0 for none) : ";
cin>>IncInfo;
for(i=0;i<G.vexnum;++i) //initial G.vertices
{ G.vertices[i].data=i;
G.vertices[i].firstarc=NULL;
}
cout<<"Plese input arc(V1-->V2), For example: (V1=1,V2=3),(V1=2,V2=4)...";
for(k=0;k<G.arcnum;++k) //input arc(v1,v2)
{ cout<<endl<<"Please input the "<<k+1<<"th arc's v1 (0<v1<G.vexnum): ";
cin>>v1;
cout<<"Please input the "<<k+1<<"th arc's v2 (0<v2<G.vexnum): ";
cin>>v2;
i=v1;
j=v2;
while(i<1||i>G.vexnum||j<1||j>G.vexnum) //if (v1,v2) illegal,again
{ cout<<endl<<"Please input the "<<k+1<<"th arc's v1 (0<v1<G.vexnum) : ";
cin>>v1;
cout<<"Please input the "<<k+1<<"th arc's v2 (0<v2<G.vexnum): ";
cin>>v2;
i=v1;
j=v2;
} //while end
i--;
j--;
ArcNode *p;
p=(ArcNode *)malloc(sizeof(ArcNode)); //allocate memory
if(!p)
{ cout<<"Overflow!"; //if overflow
return (0);
}
p->adjvex=j; //assign p
p->nextarc=G.vertices[i].firstarc;
p->info=NULL;
G.vertices[i].firstarc=p;
if(IncInfo)
{ cout<<"Please input the info :";
cin>>*(p->info); //input information
} //if end
} //for end
return (OK);
} //CreateDG() end

void DFS(ALGraph G,int v,int *visited) //DFS() sub-fuction
{ int w;
visited[v]=1;
cout<<v+1<<"->";
for(w=G.vertices[v].data;
G.vertices[v].firstarc!=NULL;
w=G.vertices[v].firstarc->adjvex,
G.vertices[v].firstarc=G.vertices[v].firstarc->nextarc)

if(visited[w]==0)
DFS(G,w,visited); //call DFS()
} //DFS() end

void DFSTraverse(ALGraph G) //DFSTraverse() sub-function
{ int v;
int visited[MAX_VERTEX_NUM];
for(v=0;v<G.vexnum;++v)
visited[v]=0; //initial visited[v]
for(v=0;v<G.vexnum;++v)
if(visited[v]==0)
DFS(G,v,visited); //call DFS()
} //DFSTraverse() end

void main() //main() function
{ ALGraph G;
cout<<endl<<endl<<"DFSTraverse.cpp";
cout<<endl<<"==============="<<endl;
CreateDG(G); //call CreateDG()
cout<<"DFS Traverse is as follows :";
cout<<endl<<endl<<"Begin->";
DFSTraverse(G); //call DFSTraverse()
cout<<"End !"<<endl<<endl<<"...OK!...";
getch();
} //main() end

这个程序编译没有问题,但是其中的FOR循环出了问题,他没有执行,哪位高手帮我看看,怎么改???
搜索更多相关主题的帖子: 遍历 深度 
2007-05-22 13:34
wujiekangyun
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-4-21
收藏
得分:0 
回复:(wujiekangyun)求助!深度优先遍历
这是附件,一起上传
2007-05-22 13:36
快速回复:求助!深度优先遍历
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034289 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved