| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 524 人关注过本帖
标题:看看我DFS为什么出不了结果?
只看楼主 加入收藏
zorrozzz
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-10-22
收藏
 问题点数:0 回复次数:1 
看看我DFS为什么出不了结果?

#include "iostream.h"

#define max 10

int visited[max];
typedef struct node{
char head;
struct node *next;
}Enode;
typedef struct vnode{
char tail;
Enode *first;
}Vertex;
typedef struct graph{
Vertex vex[max];
int v;
int e;
}algraph;
void createGraph(algraph &g){
cout<<" num of v:"<<endl;
cin>>g.v;
cout<<" num of e:"<<endl;
cin>>g.e;
for(int i=0;i<g.v;i++)
{
cout<<"enter name of vertex:"<<endl;
cin>>g.vex[i].tail;
}
for(int j=0;j<g.e;j++)
{
char v1,v2;
cout<<"enter head:"<<endl;
cin>>v1;
for(int u=0;g.vex[u].tail!=v1;u++);
cout<<"v1 :"<<u<<endl;
cout<<"enter rear:"<<endl;
cin>>v2;
for(int v=0;g.vex[v].tail!=v2;v++);
cout<<"v2:"<<v<<endl;
Enode *pe=new Enode;
pe->head=v2;
pe->next=g.vex[u].first;
g.vex[u].first=pe;
}
}

void printx(algraph g){
char v1,v2;
Enode *p;
for(int i=0;i<g.v;i++){
v1=g.vex[i].tail;
cout<<"v1:"<<v1<<endl;
p=g.vex[i].first;
//cout<<"first:"<<p<<endl;
while(p){
v2=p->head;
cout<<"("<<v1<<","<<v2<<") ";
p=p->next;
}
cout<<endl;
}
}

int getVex(algraph g,char ch){
for(int i=0;i<g.v;i++){
if(ch==g.vex[i].tail)
return i;
}
}

void DFS(algraph g,int v){
visited[v]=1;
cout<<g.vex[v].tail<<"----->";
for(Enode* p=g.vex[v].first;p;p=p->next)
{
char c=p->head;
int j=getVex(g,c);
if(!visited[j])DFS(g,j);
}
}

void DFSTraverse(algraph g){
for(int i=0;i<g.v;i++)
{
visited[i]=0;
}
for(i=0;i<g.e;i++){
if(!visited[i])DFS(g,i);
}
}

void main()
{
algraph al;
createGraph(al);
printx(al);
DFSTraverse(al);
}
好象是printx函数有问题

搜索更多相关主题的帖子: DFS 结果 
2006-11-09 23:24
菜鸟上路
Rank: 4
等 级:贵宾
威 望:14
帖 子:1120
专家分:0
注 册:2006-3-21
收藏
得分:0 
[URL=http://bbs.]http://bbs.[/URL]

2006-11-10 09:14
快速回复:看看我DFS为什么出不了结果?
数据加载中...
 
   



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

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