百度面试题:
//邻接矩阵的遍历
typedef struct{
int amount;
int *vex;
int **matrix;
}Graph;
//打印从i到j的所有回路
void PrintAllPath(Graph& G, int i, int j){
if( i > G.amount || j > G.amount || i < 0 || j < 0)
return;
//初始化访问标识
bool *IsVisted = (bool *)malloc(sizeof(bool) * G.amount);
for(int k = 0; k < G.amount; k++)
IsVisted[k] = false;
std::stack<int> s_Gnode;
s_Gnode.push(G.vex[i]);
IsVisted[i] = true;
//程序的关键是设置了pop_node和top_node;
//因为图是按照序号的从小到大访问,所以一旦有pop_node,必须从pop_node+1开始访问
int top_node;
int pop_node = -1;//最开始pop_node从0开始遍历
while( !s_Gnode.empty()){
top_node = s_Gnode.top();
int k;
for( k = pop_node+1; k < G.amount; k++ ){
if(G.matrix[top_node][k] == 1 && IsVisted[k] == false){
//如果当前节点是j节点,则打印栈中的所有元素
if(G.vex[k] == j){
s_Gnode.push(G.vex[k]);
PrintStack(s_Gnode);
pop_nod