Example input:
A B c d // the name of each vertex, char type
(CTRL + Z) // indicates that the input is complete.
7 // Number of arcs in a Directed Graph
0 1 // This number pair represents an arc 0-> 1 from the vertex numbered 0 to the vertex numbered 1.
0 2 // numbers of a, B, c, and d are 0, 1, 2, and 3, respectively.
2 0
2 3
3 0
3 1
3 2
Sample output:
Directed Graph example
"Head. h"
# Include <iostream>
# Define MAX_VERTEX_NUM 20
Using namespace std;
Class ArcBox // arc
{
Public:
ArcBox ();
Int tailvex, headvex;
ArcBox * hlink, * tlink;
Char info;
};
ArcBox: ArcBox ()
{
Tailvex = headvex = 0;
Hlink = tlink = NULL;
}
Class VexNode // Vertex
{
Public:
VexNode ();
Char data;
ArcBox * firstin, * firstout;
};
VexNode: VexNode ()
{
Firstin = firstout = NULL;
}
Class OlGraphMessage // cross linked list
{
Public:
OlGraphMessage ();
VexNode xlist [MAX_VERTEX_NUM];
Int vexnum, arcnum;
};
OlGraphMessage: OlGraphMessage ()
{
Vexnum = arcnum = 0;
}
Class OLGRAPH
{
Public:
Void OlGraphConstructor (); // create a cross linked list
Private:
Void GetVertex (); // obtain vertex information.
Void GetArcNum (); // obtain the number of radians
Void CreatOl (); // creates a cross linked list based on vertex information.
Void OlPrinter (); // output vertex Information
OlGraphMessage ol;
};
Void OLGRAPH: OlGraphConstructor ()
{
Cout <"OlGraphConstructor Called! "<Endl;
GetVertex (); // obtain vertex information.
GetArcNum (); // obtain the number of radians
CreatOl (); // create a cross linked list based on vertex Information
OlPrinter (); // output vertex Information
}
Void OLGRAPH: GetVertex () // obtain vertex information.
{
Cout <"GetVertex Called! "<Endl;
Cout <"Please Enter The Vertex" <endl;
While (cin> ol. xlist [ol. vexnum]. data)
Ol. vexnum ++;
Cin. clear ();
}
Void OLGRAPH: GetArcNum () // obtain the number of radians
{
Cout <"GetArcNum Called! "<Endl;
Cout <"Please Enter The Number Of The Arc" <endl;
Cin> ol. arcnum;
}
Void OLGRAPH: CreatOl () // create a cross linked list based on vertex Information
{
Cout <"CreatOl Called! "<Endl;
Cout <"Please Input The ArcMessage:" <endl;
Int a, B;
ArcBox * insert, * p;
While (ol. arcnum --)
{
Cin> a> B;
Insert = new ArcBox;
Insert-> headvex =;
Insert-> tailvex = B;
P = ol. xlist [a]. firstout;
If (p = NULL)
{
Ol. xlist [a]. firstout = insert;
}
Else
{
While (p-> tlink! = NULL)
P = p-> tlink;
P-> tlink = insert;
}
P = ol. xlist [B]. firstin;
If (p = NULL)
{
Ol. xlist [B]. firstin = insert;
}
Else
{
While (p-> hlink! = NULL)
P = p-> hlink;
P-> hlink = insert;
}
}
Cout <"CreatOl Succeed! "<Endl;
}
Void OLGRAPH: OlPrinter () // output vertex Information
{
Cout <"OlPrinter Called! "<Endl;
ArcBox * p;
Int od, id;
For (int I = 0; I <ol. vexnum; I ++)
{
Cout <ol. xlist [I]. data <":" <endl; // output vertex
Od = id = 0; // initialization outbound and inbound
// Obtain the arc ending with this vertex.
P = ol. xlist [I]. firstout;
While (p! = NULL)
{
Cout <ol. xlist [p-> headvex]. data <"->" <ol. xlist [p-> tailvex]. data <endl;
P = p-> tlink;
Od ++;
}
Cout <"OutDegree =" <od <endl;
// Calculate the arc with this vertex as the arc Header
P = ol. xlist [I]. firstin;
While (p! = NULL)
{
Cout <ol. xlist [p-> tailvex]. data <"<-" <ol. xlist [p-> headvex]. data <endl;
P = p-> hlink;
Id ++;
}
Cout <"InDegree =" <id <endl;
}
}
"Main. cpp"
# Include "head. h"
Int main ()
{
OLGRAPH ol;
Ol. OlGraphConstructor ();
System ("pause ");
}
Author: "Dreamer Thinker Doer"