template<
class Type>
class EdgeNode
{
public:
int tailvex;
int headvex;
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<Type> *firstin;
EdgeNode<Type> *firstout;
VertexNode(){}
VertexNode(Type d, EdgeNode<Type> *fir_in =
nullptr, EdgeNode<Type> *fir_out =
nullptr) :data(d), firstin(fir_in), firstout(fir_out){}
};
template<
class Type>
class DirectedGraph
{
private:
VertexNode<Type> OrthogonalList[MAX];
int numVertexe;
int numEdges;
int getPosition(Type el);
public:
DirectedGraph();
~DirectedGraph();
void print();
};
template<
class Type>
DirectedGraph<Type>::~DirectedGraph()
{
cout <<
"析构函数" << endl;
}
template<
class Type>
int DirectedGraph<Type>::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<Type>::DirectedGraph()
{
Type c1, c2;
int p1, p2;
int i;
EdgeNode<Type> *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<Type>(p1, p2,
nullptr,
nullptr);
if (
this->OrthogonalList[p1].firstout==
nullptr)
this->OrthogonalList[p1].firstout = enode;
else
{
EdgeNode<Type> *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<Type> *move =
this->OrthogonalList[p2].firstin;
while (move->headlink !=
nullptr)
move = move->headlink;
move->headlink = enode;
}
}
}
template<
class Type>
void DirectedGraph<Type>::print()
{
EdgeNode<Type> *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;
}