#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函数有问题