-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.c
More file actions
126 lines (109 loc) · 2.69 KB
/
Map.c
File metadata and controls
126 lines (109 loc) · 2.69 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
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
// Implementation of the Map ADT
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Map.h"
struct map {
// TODO
};
/**
* Creates a new map with the given number of cities
* Assumes that numCities is positive
*/
Map MapNew(int numCities) {
// TODO
return NULL;
}
/**
* Frees all memory associated with the given map
*/
void MapFree(Map m) {
// TODO
}
/**
* Returns the number of cities on the given map
*/
int MapNumCities(Map m) {
// TODO
return 0;
}
/**
* Returns the number of roads on the given map
*/
int MapNumRoads(Map m) {
// TODO
return 0;
}
/**
* Inserts a road between two cities with the given length
* Does nothing if there is already a road between the two cities
* Assumes that the cities are valid and are not the same
* Assumes that the length of the road is positive
*/
void MapInsertRoad(Map m, int city1, int city2, int length) {
// TODO
}
/**
* Sets the name of the given city
*/
void MapSetName(Map m, int city, char *name) {
// TODO
}
/**
* Returns the name of the given city
*/
char *MapGetName(Map m, int city) {
// TODO
return "unnamed";
}
/**
* Checks if there is a road between the two given cities
* Returns the length of the road if there is a road, and 0 otherwise
*/
int MapContainsRoad(Map m, int city1, int city2) {
// TODO
return 0;
}
/**
* Returns the number of roads connected to the given city and stores
* them in the given roads array. The `from` field should be equal to
* `city` for all the roads in the array.
* Assumes that the roads array is large enough to store all the roads
*/
int MapGetRoadsFrom(Map m, int city, Road roads[]) {
// TODO
return 0;
}
/**
* Displays the map
* !!! DO NOT EDIT THIS FUNCTION !!!
* This function will work once the other functions are working
*/
void MapShow(Map m) {
printf("Number of cities: %d\n", MapNumCities(m));
printf("Number of roads: %d\n", MapNumRoads(m));
Road *roads = malloc(MapNumRoads(m) * sizeof(Road));
if (roads == NULL) {
fprintf(stderr, "error: out of memory\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < MapNumCities(m); i++) {
printf("[%d] %s has roads to:", i, MapGetName(m, i));
int numRoads = MapGetRoadsFrom(m, i, roads);
for (int j = 0; j < numRoads; j++) {
if (j > 0) {
printf(",");
}
printf(" [%d] %s (%d)", roads[j].to, MapGetName(m, roads[j].to),
roads[j].length);
}
printf("\n");
}
free(roads);
}