int GraphList::GetFirstNeighbor(const int v)
{
if(v==-1)
return -1;
Edge *p=Head[v].adjacent;
if(p!=NULL)
return p->VerAdj;
else
return -1;
}
int GraphList::GetNextNeighbor(const int v1,const int v2)
{
if(v1!=-1&&v2!=-1)
{
Edge *p=Head[v1].adjacent;
while(p->VerAdj!=v2&&p!=NULL)
p=p->link;
if(p==NULL) return -1;
p=p->link;
if(p==NULL) return -1;
return p->VerAdj;
}
return -1;
}
void GraphList::RDFS(const int v,int *visited)
{
cout<<v<<” “;
visited[v]=1;
int w=GetFirstNeighbor(v);
while(w!=-1)
{
if(!visited[w])
RDFS(w,visited);
w=GetNextNeighbor(v,w);
}
}
void GraphList::DepthFirstSearch()
{
int *visited=new int[graphSize];
for(int k=0;k<graphSize;k++)
visited[k]=0;
RDFS(0,visited);
delete[] visited;
}