算法用C++实现
为什么要写这个算法呢?苦于在网上查找只有邻接矩阵的实现,所以自己弄了一个邻接链表的实现,可以提高速率
实现方式是使用三个栈 其中一个栈用来遍历所有顶点,第二个栈用于记录第一个栈的父母结点,第三个栈用来记录正在遍历的其中一条路径
需要三个路径的原因是:每次将一个结点加入进第三个栈时,需要确定第三个栈顶元素是加入元素的父母结点
还需要一个数组用来记录结点是否被访问过
template <class T>
bool Picture<T>::findway()
{
cout<<"Input:";
T first,second;
cin>>first>>second;
cout<<"Output:"<<endl;
int n0,n1;
n0=getVerticePos(first);
n1=getVerticePos(second);
if(n0==-1||n1==-1) //不存在的结点
return false;
bool *judge=new bool[numVertices]; //判断结点是否走过
for(int i=0;i<numVertices;i++)
judge[i]=false;
Edge *loc;
stack <int> s; //遍历各结点
stack <int> parent; //记录前一结点
stack <int> q; //记录其中一条
s.push(n0);
parent.push(-1);
int i,j,length;
int path[50];
while(!s.empty())