标题:看看我DFS为什么出不了结果?
取消只看楼主
zorrozzz
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-10-22
 问题点数:0 回复次数:0 
看看我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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-101899-1-1.html




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

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