-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
279 lines (217 loc) · 7.62 KB
/
main.cpp
File metadata and controls
279 lines (217 loc) · 7.62 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <stdio.h>
#include "linked_list.h"
#include "priority_queue.h"
//#include "directed_graph.h"
#include <iostream>
#include <string>
#include <cstring>
class Animal{
public:
Animal(std::string str){
this->name = str;
}
std::string getName(){
return this->name;
}
int compare(Animal a){
return std::strcmp(this->name.c_str(), a.getName().c_str());
}
private:
std::string name;
};
int main(int argc, char** argv)
{
LinkedList<Animal*> AList = LinkedList<Animal*>();
Animal* a = new Animal("Fluffy");
Animal* b = new Animal("Scruffy");
Animal* c = new Animal("Moe");
AList.add_last(a);
AList.add_first(b);
AList.add_at(1,c);
for (int i = 0; i < AList.getSize(); ++i) {
Animal* x = AList.get(i);
std::cout << i << ": " << x->getName() << "\n";
}
Animal* d = AList.remove(1);
Animal* e = AList.remove_first();
Animal* f = AList.remove_last();
std::cout << "First: " << e->getName() << "\n";
std::cout << "Middle: " << d->getName() << "\n";
std::cout << "Last: " << f->getName() << "\n";
/// delete d;
// delete e;
// delete f;
AList.add_last(a);
AList.add_first(b);
AList.add_at(1,c);
Animal* g = AList.getFirst();
Animal* h = AList.getLast();
std::cout << "First: " << g->getName() << "\n";
std::cout << "Last: " << h->getName() << "\n";
// delete g;
// delete h;
int aIdx = AList.index_of(a);
int bIdx = AList.index_of(b);
int cIdx = AList.index_of(c);
// std::cout << "Index of Fluffy (expected 2): " << aIdx << "\n";
// std::cout << "Index of Moe (expected 1): " << cIdx << "\n";
// std::cout << "Index of Scruffy (expected 0): " << bIdx << "\n";
std::cout << "swapping 0 and 2\n";
AList.swap(0,2);
std::cout << "Index of Fluffy (expected 0): " << AList.index_of(a)<< "\n";
std::cout << "Index of Scruffy (expected 2): " << AList.index_of(b) << "\n";
std::cout << "swapping 0 and 2 ... again\n";
AList.swap(2,0);
std::cout << "Index of Fluffy (expected 2): " << AList.index_of(a)<< "\n";
std::cout << "Index of Scruffy (expected 0): " << AList.index_of(b) << "\n";
printf("\nPriority Queue Testing\n\n");
PriorityQueue<Animal*, int>* pq = new PriorityQueue<Animal*, int>();
// Need to determine how to compare objects.
// Override an operator? Do all primitives and std::string support operator < ?
pq->enqueue(a, 0);
std::cout << "added " << a->getName() << "\n";
// std::cout << "Animal peeked is (expected Fluffy): " << pq->peek()->getName() << "\n";
pq->enqueue(b, 1);
std::cout << "added " << b->getName() << "\n";
// std::cout << "Peeking after added Fluffy and Scruffy with priorities 0 and 1\n";
// std::cout << "Animal peeked is (expected Scruffy): " << pq->peek()->getName() << "\n";
pq->enqueue(c, 2);
std::cout << "added " << c->getName() << "\n";
Animal* ab = new Animal("Jim");
pq->enqueue(ab, 5);
Animal* ac = new Animal("Bob");
pq->enqueue(ac, 3);
Animal* ad = new Animal("Beth");
pq->enqueue(ad, 2);
Animal* ae = new Animal("Becky");
pq->enqueue(ae, 2);
Animal* cd = new Animal("Larry");
Animal* mo = new Animal("Moe");
std::cout << "Contains Beth? expected true: " << pq->contains(ad) << "\n";
std::cout << "Contains Larry? epected false: " << pq->contains(cd) << "\n";
std::cout << "Contains a new Moe? epected false: " << pq->contains(mo) << "\n";
std::cout << "Dequeue (expected Jim) " << pq->dequeue()->getName()<< "\n";
std::cout << "Dequeue (expected Bob) "<< pq->dequeue()->getName()<< "\n";
std::cout << "Dequeue (expected Moe) " << pq->dequeue()->getName()<< "\n";
std::cout << "Dequeue (expected(Beth) " << pq->dequeue()->getName()<< "\n";
std::cout << "Dequeue (expected Becky) " << pq->dequeue()->getName()<< "\n";
std::cout << "Dequeue (expected Scruffy) " << pq->dequeue()->getName()<< "\n";
std::cout << "Dequeue (expected Fluffy) " << pq->dequeue()->getName()<< "\n";
std::cout << "list size (expected 0): " << pq->getSize() << "\n";
/*
LinkedList<int>* list = new LinkedList<int>();
list->add_first(0);
for(int i = 1; i < 99; i++){
list->add_at(i,i);
}
list->add_last(99);
int size = list->getSize();
for (int i = 0; i < size; ++i) {
int item = list->get(i);
printf("%d\n", item);
}
int first = list->remove_first();
int last = list->remove_last();
printf("first %d\n", first);
printf("last %d\n", last);
for (int i = list->getSize()-1; i >=0; i--){
int element = list->remove(i);
printf("%d\n", element);
}
*/
// Instantiating a DirectedGraph struct.
/* DirectedGraph* digraph = initialize_digraph(sizeof(char),"char");
// Creating elements to add to graph.
char* a = "a";
char* b = "b";
char* c = "c";
char* d = "d";
char* e = "e";
char* f = "f";
// Adding elements to graph.
add_vertex(digraph, a);
add_vertex(digraph, b);
add_vertex(digraph, c);
add_vertex(digraph, d);
add_vertex(digraph, e);
add_vertex(digraph, f);
// Creating edges between vertices and assigning weights.
add_arc(digraph, a, b, 4);
add_arc(digraph, a, e, 2);
add_arc(digraph, a, f, 1);
add_arc(digraph, b, c, 5);
add_arc(digraph, b, a, 6);
add_arc(digraph, c, e, 4);
add_arc(digraph, d, e, 3);
add_arc(digraph, d, b, 2);
add_arc(digraph, e, d, 3);
add_arc(digraph, e, b, 2);
add_arc(digraph, e, f, 1);
add_arc(digraph, f, d, 3);
*/
/*
* The code below demonstrates how to create a DiGraph from a csv file, create, retrieve and print the
* adjacency matrix, print the String representation of a DiGraph to standard output, and compute and print
* the all pairs' shortest paths algorithm (Floyd-Warshall) to standard output.
*/
// DirectedGraph* digraph = create_digraph_from_file((char*)"test_adjacency.csv");
// Retrieving a linkedlist of the vertices.
/*LinkedList* vertexList = get_vertices(digraph);
// Calling the void function to create the adjacency matrix for the digraph.
create_adjacency_matrix(digraph);
// Retrieving the adjacecny matrix as a 2D array.
float** adjMtx = get_adjacency_matrix(digraph);
// Formatting for adjacency matrix printing.
printf(" ");
for(int i = 0; i < linked_list_size(vertexList); i++)
{
Vertex* u = (Vertex*)linked_list_get(vertexList, i);
printf("%d ", *(int*) get_data(u));
}
printf("\n");
// Printing adjacency matrix.
for(int i = 0; i < digraph_size(digraph); i++)
{
Vertex* u = (Vertex*)linked_list_get(vertexList, i);
printf("%d | ", *(int*) get_data(u));
for(int j = 0; j < digraph_size(digraph); j++)
{
Vertex* v = (Vertex*)linked_list_get(vertexList, j);
// If this edge weight is less than my max edge weight, print 0.
if(!(has_arc_to_vertex(u, v)))
{
printf("0 ");
// Otherwise print the edge value.
} else {
printf("%f ", adjMtx[i][j]);
}
}
printf("\n");
}
printf("\n");
// The code below prints a String representation of a Digraph to standard output.
std::cout << *digraph << std::endl;
// Calling all pairs shortest paths algorithm, retrieved as a 2D array.
float** paths = all_pairs_shortest_paths(digraph);
// Formatting for all pairs shortest paths matrix.
printf(" ");
for(int i = 0; i < linked_list_size(vertexList); i++)
{
Vertex* u = (Vertex*)linked_list_get(vertexList, i);
printf("%s ", (char*)get_data(u));
}
printf("\n");
// Printing all pairs shortest paths matrix.
for(int i = 0; i < digraph_size(digraph); i++)
{
Vertex* v = (Vertex*)linked_list_get(vertexList, i);
printf("%s | ",(char*)get_data(v));
for(int j = 0; j < digraph_size(digraph); j++)
{
printf("%.0f ", paths[i][j]);
}
printf("\n");
}
*/
return 0;
}