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
|
#include <stdio.h>
#include <stdlib.h>
typedef struct Road{
int u, v;
double dist;
} Road;
typedef struct RoadCell{
Road* road;
struct RoadCell* next;
} RoadCell;
typedef struct City{
int id;
int x, y;
RoadCell* roads;
} City;
City** init_cities(int n){
City** cities = (City**) malloc(sizeof(City)*n);
for (int i = 0; i < n; i++){
cities[i] = (City*) malloc(sizeof(City));
cities[i]->id = i;
cities[i]->x = 0;
cities[i]->y = 0;
cities[i]->roads = NULL;
}
return cities;
}
void update_city(City* city, int x, int y){
city->x = x;
city->y = x;
}
void add_route(City* c1, City* c2, double dist){
Road* road = (Road*) malloc(sizeof(Road));
road->u = c1->id;
road->v = c2->id;
road->dist = dist;
RoadCell* road_1 = (RoadCell*) malloc(sizeof(RoadCell));
road_1->road = road;
road_1->next = c1->roads;
c1->roads = road_1;
RoadCell* road_2 = (RoadCell*) malloc(sizeof(RoadCell));
road_2->road = road;
road_2->next = c2->roads;
c2->roads = road_2;
}
void print(City** cities, int n){
printf("Cities len: %d\n", n);
for (int i = 0; i < n; i++){
printf("city %d: {x: %d, y: %d}, roads [", i, cities[i]->x, cities[i]->y);
RoadCell* roads = cities[i]->roads;
while (roads){
Road* road = roads->road;
printf("{from: %d, to: %d, dist: %f},", road->u, road->v, road->dist);
roads = roads->next;
}
printf("]\n");
}
}
void free_cities(City** cities, int n){
for (int i = 0; i < n; i++) {
City* city = cities[i];
while (city->roads){
//TODO: free(city->roads->road)
RoadCell* old = city->roads;
city->roads = city->roads->next;
free(old);
}
free(city);
}
free(cities);
}
int main(){
City** cities = init_cities(4);
add_route(cities[0], cities[1], 3);
add_route(cities[1], cities[2], 4);
add_route(cities[0], cities[2], 8);
add_route(cities[3], cities[1], 4.5);
print(cities, 4);
return 0;
}
|