資源簡介
這是包含所有數據結構四次實驗的源碼,在大家不會的時候可以給大家參考,但是不希望大家去抄襲,態度決定高度!
代碼片段和文件信息
#include“mgraph.h“
#include
template
class?ExtMGraph:public?MGraph
{
public:
ExtMGraph(int?mSizeconst?T?noedg):MGraph(mSizenoedg){}????????????//調用父類構造函數
void?DFS();
void?BFS();
private:
void?DFS(int?ubool*?visited);
void?BFS(int?ubool*?visited);?
};
template
void?ExtMGraph::DFS()
{
bool?*visited?=?new?bool[n];
for(int?i=0;i visited[i]?=?false;
for(i=0;i if(visited[i]==false)
DFS(ivisited);
delete[]?visited;
}
template
void?ExtMGraph::BFS()
{
bool?*visited?=?new?bool[n];
for(int?i=0;i visited[i]?=?false;
for(i=0;i if(visited[i]==false)
BFS(ivisited);
delete[]?visited;
}
template
void?ExtMGraph::DFS(int?ubool*?visited)
{
visited[u]?=?true;
cout< for(int?i=0;i {
if(a[u][i]!=noEdge&&visited[i]==false)
DFS(ivisited);
}
}
template
void?ExtMGraph::BFS(int?ubool*?visited)
{
queue?q;
visited[u]?=?true;
cout< q.push(u);
while(q.empty()==false)
{
u=q.front();
q.pop();
for(int?i=0;i {
if(a[u][i]!=noEdge&&visited[i]==false)
{
visited[i]?=?true;
cout< q.push(i);
}
}
}
}
int?main()
{
ExtMGraph?extMGraph(8-1);
extMGraph.Insert(071);
extMGraph.Insert(061);
extMGraph.Insert(051);
extMGraph.Insert(741);
extMGraph.Insert(621);
extMGraph.Insert(531);
extMGraph.Insert(341);
extMGraph.Insert(411);
cout<<“DFS()函數輸出結果:“< extMGraph.DFS();
cout<<“\nBFS()函數輸出結果:“< extMGraph.BFS();
cout< return?0;
}
- 上一篇:office2007上機練習題庫很全、很實用
- 下一篇:MSP430入門源碼
評論
共有 條評論