C++クロスチェーンテーブル実現有向図
9878 ワード
/********************* ******************/
//
template<class Type>
class EdgeNode
{
public:
int tailvex;//
int headvex;//
//int weight;
EdgeNode *headlink;// ,
EdgeNode *taillink;// ,
EdgeNode(int tailv, int headv, EdgeNode *headl = nullptr, EdgeNode *taill = nullptr) :tailvex(tailv), headvex(headv), headlink(headl), taillink(taill){}
};
//
template<class Type>
class VertexNode
{
public:
Type data;//
EdgeNode *firstin;//
EdgeNode *firstout;//
VertexNode(){}
VertexNode(Type d, EdgeNode *fir_in = nullptr, EdgeNode *fir_out = nullptr) :data(d), firstin(fir_in), firstout(fir_out){}
};
// ( )
template<class Type>
class DirectedGraph
{
private:
VertexNode OrthogonalList[MAX];//
int numVertexe;//
int numEdges;//
int getPosition(Type el);// el
public:
DirectedGraph();//
~DirectedGraph();//
void print();
};
//
template<class Type>
DirectedGraph::~DirectedGraph()
{
cout << " " << endl;
}
// el
template<class Type>
int DirectedGraph::getPosition(Type el)
{
int i;
for (i = 0; i < this->numVertexe; i++)
{
if (this->OrthogonalList[i].data == el)
return i;
}
return -1;
}
// ( )
template<class Type>
DirectedGraph::DirectedGraph()
{
Type c1, c2;
int p1, p2;
int i;
EdgeNode *enode;//
cout << "Input vertexe number: ";
cin >> this->numVertexe;
cout << "Input edge number: ";
cin >> this->numEdges;
//
for (i = 0; i < this->numVertexe; i++)
{
cout << "Input a Type data(" << i + 1 << "): ";
cin >> this->OrthogonalList[i].data;
this->OrthogonalList[i].firstin = nullptr;
this->OrthogonalList[i].firstout = nullptr;
}
//
//
for (i = 0; i < this->numEdges; i++)
{
cout << "edge" << i << endl;
cout << "Input arcHead: ";
cin >> c1;
cout << "Input arcTail: ";
cin >> c2;
p1 = getPosition(c1);
p2 = getPosition(c2);
enode = new EdgeNode(p1, p2, nullptr, nullptr);//
if (this->OrthogonalList[p1].firstout==nullptr)
this->OrthogonalList[p1].firstout = enode;
else
{
EdgeNode *move = this->OrthogonalList[p1].firstout;
while (move->taillink != nullptr)
move = move->taillink;
move->taillink = enode;
}
if (this->OrthogonalList[p2].firstin==nullptr)
this->OrthogonalList[p2].firstin = enode;
else
{
EdgeNode *move = this->OrthogonalList[p2].firstin;
while (move->headlink != nullptr)
move = move->headlink;
move->headlink = enode;
}
}
}
//
template<class Type>
void DirectedGraph::print()
{
EdgeNode *move;
cout << " :" << endl;
for (int i = 0; i < this->numVertexe; i++)
{
cout << "Vertexe( " << i << " ): ";
move = this->OrthogonalList[i].firstout;
while (move != nullptr)
{
cout << move->headvex << " ";
move = move->taillink;
}
cout << endl;
}
cout << " :" << endl;
for (int i = 0; i < this->numVertexe; i++)
{
cout << "Vertexe( " << i << " ): ";
move = this->OrthogonalList[i].firstin;
while (move != nullptr)
{
cout << move->tailvex << " ";
move = move->headlink;
}
cout << endl;
}
}
//
int main()
{
DirectedGraph<char> graph;
graph.print();
return 0;
}