-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataStructures.h
131 lines (114 loc) · 2.7 KB
/
DataStructures.h
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
// Pranav Nair
// CS 3345.501
// Professor: Kamran Khan
// Due Date: December 6, 2021
#include <string>
using namespace std;
/* Edge will be used for a linked list representing the outgoing edges of a vertex. It keeps track of the destination city, time and cost */
class Edge {
private:
string destCity;
int time;
float cost;
Edge* next;
public:
string getDestCity(){
return destCity;
}
int getTime(){
return time;
}
float getCost(){
return cost;
}
Edge* getNext(){
return next;
}
void setNext(Edge* e){
next = e;
}
Edge(string d, int t, float c){
destCity = d;
time = t;
cost = c;
next = NULL;
}
};
/* Class for the linked list of vertices. This is the "linked lists of linked lists."
In addition to a link for the next vertex, each node also has a pointer to an Edge which represents the adjacency list of the vertex.
The same class can be used to represent paths as well, by keeping adjacency lists as empty */
class Vertex{
private:
string sourceCity;
Edge* destinations;
Vertex* next;
public:
string getSourceCity(){
return sourceCity;
}
Vertex* getNext(){
return next;
}
void setNext(Vertex* v){
next = v;
}
Edge* getDestinations(){
return destinations;
}
void setDestinations(Edge* d){
destinations = d;
}
void addDestination(Edge* d){
d->setNext(destinations);
destinations = d;
}
Vertex(string s){
sourceCity = s;
destinations = NULL;
next = NULL;
}
};
// IMPORTANT NOTE (to TA): Professor Khan allowed using built-in stacks according to his email reply to me.
/* Class used for the backtracking algorithm
This class represents a path with the cumulative time and cost */
class stackNode{
private:
Vertex* v;
int time;
float cost;
public:
stackNode(Vertex* ver){
v = ver;
if (ver != NULL){ // If there is a vertex and it isn't an empty path
time = 0;
cost = 0;
}
else{
time = -1;
cost = -1;
}
}
stackNode(Vertex* ver, int t, float c){
v = ver;
time = t;
cost = c;
}
int getTime(){
return time;
}
float getCost(){
return cost;
}
Vertex* getPath(){
return v;
}
// Returns a formatted path in string
string getPathString(){
string s;
s = v->getSourceCity() + ". ";
for(Vertex* ver = v->getNext(); ver; ver = ver->getNext()){
s = ver->getSourceCity() + " -> " + s;
}
return s;
}
};