Figure data structure graph.h

xiaoxiao2021-03-06  92

///

///

// Figure data structure graph.h //

///

//

#include

#include "queue.h"

Template Class Graph;

Template struct node

{

Friend class graph ;

Int Num;

DiSType Val;

Node * Next;

}

Template struct gpnode

{

Friend class graph ;

Nametype Data;

Node * link;

}

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 * table;

Node * p;

INT NumNode; // Number of nodes

}

Template

Void graph :: creat ()

{

DO

{

COUT << "Please enter the number of nodes:";

Numnode;

} while (numnode <= 0);

Table = new gpnode [NumNode];

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 :: printnode ()

{

COUT << "The node data items in the picture:";

For (int i = 0; i

COUT << Table.Data << "

Cout << Endl;

}

Template

Void graph :: DFS ()

{

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 :: DFS (int v, int visited [])

{

Node * t;

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 :: bfs ()

{

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 void graph :: bfs (int v, int visited [])

{

Queue q;

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;

}

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-102110.html

New Post(0)