-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphl.cpp
131 lines (120 loc) · 3.38 KB
/
graphl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "graphl.h"
#include <iomanip>
using namespace std;
//------------------ GraphL() ------------------
//default constructor, initializes all values
//Preconditions:
//Postconditions:
GraphL::GraphL() {
size = 0;
for (int i = 1; i < MAX; i++) {
arr[i].visited = false;
arr[i].data = NULL;
arr[i].edgeHead = NULL;
}
}
// ------------------------ ~GraphL() ---------------
//destructor, de-allocates pointers from the memory
//Preconditions:
//Postconditions:
GraphL::~GraphL() {
for (int i = 1; i <= size; i++) {
arr[i].visited = false;
arr[i].data = NULL;
delete arr[i].data;
//traverse through the list to delete all nodes
if (arr[i].edgeHead != NULL)
{
EdgeNode* node = arr[i].edgeHead;
arr[i].edgeHead = arr[i].edgeHead->nextEdge; //move pointer/edgeHead
node = NULL;
delete node;
}
}
}
// --------------------------- buildGraph(istream& in) --------------
//builds the graph
//Preconditions: the file passed is in the valid format
//Postconditions: builds the graph successfully
void GraphL::buildGraph(istream& in) {
//get the size
in >> size;
string name = "";
getline(in, name); //read the lines in the file
for (int i = 1; i <= size; i++)
{
getline(in, name);
NodeData* tmp = new NodeData(name);
arr[i].data = tmp;
}
//know the start and end points
int start, end;
while (in >> start >> end)
{
if (start == 0)
break;
if (arr[start].edgeHead == NULL)
{
EdgeNode* node = new EdgeNode;
node->adjGraphNode = end;
arr[start].edgeHead = node;
arr[start].edgeHead->nextEdge = NULL;
}
else
{
EdgeNode* node = new EdgeNode;
node->adjGraphNode = end;
node->nextEdge = arr[start].edgeHead;
arr[start].edgeHead = node;
}
}
}
//----------------------------- displayGraph() ----------------------
//displays all the elements of the graph
//Preconditions: graph already exists
//Postconditions: creates an adjacency list, and prints it out.
void GraphL::displayGraph() {
cout << "Graph: " << endl;
for (int i = 1; i <= size; i++)
{
cout << "Node" << i << " " << *arr[i].data << endl;
cout << endl;
EdgeNode* node = arr[i].edgeHead;
while (node != NULL)
{
cout << setw(7) << "edge " << i << setw(2) << node->adjGraphNode << endl;
node = node->nextEdge; //move pointer
}
}
}
//--------------------------- depthFirstSearchHelper(int num) ---------------------
//helper for depth first search
//Preconditions: none.
//Postconditions: the DFS order will be outputed recursively
void GraphL::depthFirstSearchHelper(int vis) {
//print the node to get the print correct
cout << setw(2) << vis;
//set visited so that it wont be called again
arr[vis].visited = true;
//traverse the list
EdgeNode* node = arr[vis].edgeHead;
while (node != NULL)
{
if (!arr[node->adjGraphNode].visited)
depthFirstSearchHelper(node->adjGraphNode);
node = node->nextEdge; //move pointer
}
}
//--------------------------- depthFirstSearch() ---------------------
//prints the depth first search of a graph
//Preconditions: graph exists.
//Postconditions: the DFS order will be outputed
void GraphL::depthFirstSearch() {
cout << "Depth-first ordering: ";
for (int i = 1; i <= size; i++)
{
//if the node is not yet visited then recurse using helper
if (!arr[i].visited)
depthFirstSearchHelper(i);
}
}