权当备忘了
主要参考《数据结构严尉敏版》
但是是c++实现
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
#include<iostream>
#include<vector>
using namespace std;
class AdjListGraph
{
public:
AdjListGraph();
~AdjListGraph();
int NumberOfVertex() //query for number of vertex
{
return VexNum;
}
int NumberOfArc() //query for number of arc
{
return ArcNum;
}
void DFSTraverse();
void BFSTraverse();
private:
struct ArcNode
{
ArcNode(int aAdjVex = -1, ArcNode *aNextArc = NULL, int aInfo = -1)
: AdjVex(aAdjVex), NextArc(aNextArc), info(aInfo) {}
int AdjVex; //当前弧指向的节点位置
ArcNode *NextArc; //指向下一条弧的指针
int info; //该弧相关信息的指针
};
struct VNode
{
VNode(int vData = -1, ArcNode *vFirstArc = NULL)
: data(vData), FirstArc(vFirstArc) {}
int data; //information of the Node
ArcNode *FirstArc; //the first arc adjacent to the Node
};
static const int MaxVextexNum = 100;
VNode AdjList[MaxVextexNum];
int VexNum; //number of Vetices
int ArcNum; //number of arcs
int kind; //kind of graph, 0 stand for
//undigraph, 1 for digraph
static const int NoPosition = -1;
bool visited[MaxVextexNum];
int FirstAdjVex(int vex);
int NextAdjVex(int vex, int curVex);
void DFS(int vex);
};
AdjListGraph::AdjListGraph()
{
cout << "Please input the VexNum and ArcNum of the Graph in order: " << endl;
cout << "VexNum: ";
cin >> VexNum;
cout << "ArcNum: ";
cin >> ArcNum;
cout << "Next, pleast input the information of each vertex: " << endl;
for(int i = 0; i < VexNum; i++)
{
cout << "the information of " << i
<< "th vertex: ";
cin >> AdjList[i].data;
}
cout << "Next, pleast input the information of the arc:"
<< endl;
for(int i = 0; i < ArcNum; i++)
{
int begin;
int end;
cout << "Pleast input the begin and end position of "
<< i << "th arc: " << endl;
cout << "begin position: ";
cin >> begin;
cout << "end position: ";
cin >> end;
ArcNode *ptrArc1 = new ArcNode(end, AdjList[begin].FirstArc);
AdjList[begin].FirstArc = ptrArc1;
ArcNode *ptrArc2 = new ArcNode(begin, AdjList[end].FirstArc);
AdjList[end].FirstArc = ptrArc2;
}
}
AdjListGraph::~AdjListGraph()
{
for(int i = 0; i < VexNum; i++)
{
ArcNode *p;
p = AdjList[i].FirstArc;
if(p != NULL)
{
ArcNode *q = p;
p = p->NextArc;
delete q;
}
}
}
void AdjListGraph::DFSTraverse()
{
int vex;
for(vex = 0; vex < VexNum; vex++)
visited[vex] = false;
for(vex = 0; vex < VexNum; vex++)
if(!visited[vex])
DFS(vex);
}
inline int AdjListGraph::FirstAdjVex(int vex)
{
return AdjList[vex].FirstArc->AdjVex;
}
int AdjListGraph::NextAdjVex(int vex, int curVex)
{
ArcNode *ptrArc = AdjList[vex].FirstArc;
while(ptrArc->NextArc != NULL)
{
if(ptrArc->AdjVex == curVex)
return ptrArc->NextArc->AdjVex;
else
ptrArc = ptrArc->NextArc;
}
return NoPosition;
}
void AdjListGraph::DFS(int vex)
{
visited[vex] = true;
cout << "Vex: " << vex << " Vex->data: " << AdjList[vex].data << endl;
for(int w = FirstAdjVex(vex); w >= 0; w = NextAdjVex(vex, w))
{
if(!visited[w])
DFS(w);
}
}
int main()
{
AdjListGraph G;
G.DFSTraverse();
return 0;
}