///
///
// Figure data structure graph.h //
///
//
#include
#include "queue.h"
Template
Template
{
Friend class graph
Int Num;
DiSType Val;
Node
}
Template
{
Friend class graph
Nametype Data;
Node
}
Template
Class graph
{
PUBLIC:
Void creat (); // creation map
Void printnode (); // Minute data items in the print map
Void DFS (); // Diagram depth priority search, main process
Void DFS (int V, int visited []); // sub-process
Void BFS (); // Parent-Diagram of Parent Search, Main Process
Void BFS (int V, int visited []); // child process
Void shortpath (); // ask for the shortest path
Private:
GPNode
Node
INT NumNode; // Number of nodes
}
Template
Void graph
{
DO
{
COUT << "Please enter the number of nodes:";
Numnode;
} while (numnode <= 0);
Table = new gpnode
Cout << "Please enter each node data item" << ENDL;
For (int i = 0; i { CIN >> TABLE.DATA; Table.Link = NULL; } Cout << "Please enter the relationship between each side (such as: a b)" << endl; i = 1; Nametype nodea, nodeb; Bool Finda, Findb; CHAR ISEXIT; Int M, N; DO { Finda = findb = false; Cout << "Please enter the" << i << "to the side of the relationship" << Endl; Nodeb; For (m = 0, n = 0; M { IF (Nodea! = Table [M] .data) M ; Else FINDA = TRUE; IF (Nodeb! = Table [N] .data) n ; Else FINDB = true; } IF (! (finda & findb)) Cout << "The input node data item has an error" << ENDL; Else { P = new node P-> Next = Table [M] .LINK; P-> Num = n; Table [M] .LINK = P; Cout << "Please enter the right value of this pair:"; CIN >> P-> VAL; i ; } Cout << "Whether to enter: Y) Continue, x) Any key to exit"; ISEXIT; IF (ISEXIT! = 'Y' && ISEXIT! = 'Y') Break; WHILE (TRUE); } Template Void graph { COUT << "The node data items in the picture:"; For (int i = 0; i COUT << Table.Data << " Cout << Endl; } Template Void graph { INT * Visited = new int [NumNode]; Depth Priority Search for COUT << ":"; For (int i = 0; i Visited = 0; For (i = 1; i DFS (I, Visited); DELETE [] Visited; Cout << Endl; } Template Void graph { Node IF (Visited [V] == 0) COUT << Table [V] .data << " Visited [V] = 1; T = Table [V] .LINK; While (t! = null) { IF (Visited [T-> Num] == 0) DFS (T-> Num, Visited); T = T-> Next; } } Template Void graph { INT * Visited = new int [NumNode]; Singitude Search of COUT << "map:" For (int i = 0; i Visited = 0; For (i = 0; i BFS (I, Visited); } Template { Queue Int n; IF (Visited [V] == 0) { Visited [V] = 1; COUT << Table [V] .data << " Q.enqueue (v); While (! q.isempty ()) { n = q.delqueue (); P = Table [N] .link; While (p! = null) { n = p-> Num; IF (Visited [N] == 0) { COUT << Table [N] .data << " Visited [n] = 1; } P = P-> next; } } } }