最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

非递归深度遍历

来源:动视网 责编:小OO 时间:2025-09-26 11:28:22
文档

非递归深度遍历

非递归深度遍历.txt9母爱是一滴甘露,亲吻干涸的泥土,它用细雨的温情,用钻石的坚毅,期待着闪着碎光的泥土的肥沃;母爱不是人生中的一个凝固点,而是一条流动的河,这条河造就了我们生命中美丽的情感之景。#ifndefALGraph_CPP#defineALGraph_CPP#include"ALGraph.h"templateALGraph::ALGraph(vectorvs){m_N=vs.size();m_Data.resize(m_N);for(inti=0;iweight=es[i].we
推荐度:
导读非递归深度遍历.txt9母爱是一滴甘露,亲吻干涸的泥土,它用细雨的温情,用钻石的坚毅,期待着闪着碎光的泥土的肥沃;母爱不是人生中的一个凝固点,而是一条流动的河,这条河造就了我们生命中美丽的情感之景。#ifndefALGraph_CPP#defineALGraph_CPP#include"ALGraph.h"templateALGraph::ALGraph(vectorvs){m_N=vs.size();m_Data.resize(m_N);for(inti=0;iweight=es[i].we
非递归深度遍历.txt9母爱是一滴甘露,亲吻干涸的泥土,它用细雨的温情,用钻石的坚毅,期待着闪着碎光的泥土的肥沃;母爱不是人生中的一个凝固点,而是一条流动的河,这条河造就了我们生命中美丽的情感之景。#ifndef ALGraph_CPP

#define ALGraph_CPP

#include  "ALGraph.h"

template

ALGraph::ALGraph(vector vs)

{ m_N = vs.size();

  m_Data.resize(m_N);

for(int i=0; i  { m_Data[i].data=vs[i];

    m_Data[i].firstarc=NULL;

  }

}

template

ALGraph::~ALGraph()

{ for(int i=0; i    Clear(m_Data[i].firstarc);

}  

template

void ALGraph::Clear(ArcNode *head)

{ while(head)

{ ArcNode *p=head->nextarc;

    delete head; head=p;

  }

}

template

void ALGraph::Append(vector &es)

{ for(int i=0; i  { int v1=es[i].v1, v2=es[i].v2;

    ArcNode *p=new ArcNode;

p->adjvex=v2; p->weight=es[i].weight;

p->nextarc=m_Data[v1].firstarc;

    m_Data[v1].firstarc = p;

  }

}

template

void ALGraph::Output()

{ for(int i=0; i { cout< for(ArcNode *p=m_Data[i].firstarc; p; p=p->nextarc)

cout< cout<  }

cout<

template

vector ALGraph::DFSTraverse()

{ vector vs,vs1;

vector visited(m_N,false);

for(int v=0; v    if(visited[v]==false)       

    { vs1.clear();

      DoDFSTraverse(v,visited,vs1);

      vs.insert(vs.end(),vs1.begin(),vs1.end());

    }

  return vs;

}  

template

void ALGraph::DoDFSTraverse(int v,vector& visited,vector& vs)

{ stack S;

  // 约定每个顶点被访问之后,再进栈

  vs.push_back(m_Data[v].data); visited[v]=true;

  Status s1={v,m_Data[v].firstarc};  S.push(s1);

  while( !S.empty() )

  { Status &s=S.top(); // s是栈顶状态的引用

    // 寻找v的下一个未访问过的邻接点

for(; s.p; s.p=s.p->nextarc)

if(visited[s.p->adjvex]==false) break;

    if(s.p)   

{ int w=s.p->adjvex; // 顶点w未访问过

      vs.push_back(m_Data[w].data); visited[w]=true;

      Status nexts;

      nexts.v=w;  nexts.p=m_Data[w].firstarc;

      S.push(nexts); 

     }

     else 

       S.pop();   // 以v为起点的搜索已经完毕

   }  

}

#endif

文档

非递归深度遍历

非递归深度遍历.txt9母爱是一滴甘露,亲吻干涸的泥土,它用细雨的温情,用钻石的坚毅,期待着闪着碎光的泥土的肥沃;母爱不是人生中的一个凝固点,而是一条流动的河,这条河造就了我们生命中美丽的情感之景。#ifndefALGraph_CPP#defineALGraph_CPP#include"ALGraph.h"templateALGraph::ALGraph(vectorvs){m_N=vs.size();m_Data.resize(m_N);for(inti=0;iweight=es[i].we
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top