-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFb.c
More file actions
233 lines (186 loc) · 5.25 KB
/
Fb.c
File metadata and controls
233 lines (186 loc) · 5.25 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
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
// Implementation of the FriendBook ADT
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Fb.h"
#include "List.h"
#include "Map.h"
#include "Queue.h"
#define DEFAULT_CAPACITY 1 // DO NOT change this line
struct adjNode {
int v;
struct adjNode *next;
};
// DO NOT modify this struct
struct fb {
int numPeople;
int capacity;
char **names; // the id of a person is simply the index
// that contains their name in this array
Map nameToId; // maps names to ids
struct adjNode **adj; // adjacency lists, kept in increasing order
};
static void increaseCapacity(Fb fb);
static int nameToId(Fb fb, char *name);
static struct adjNode *newAdjNode(int v);
static bool inAdjList(struct adjNode *l, int v);
static void freeAdjList(struct adjNode *l);
// Please ignore this line
static struct adjNode * __attribute__((unused)) newAdjNode(int v);
////////////////////////////////////////////////////////////////////////
// Creates a new instance of FriendBook
Fb FbNew(void) {
Fb fb = malloc(sizeof(*fb));
if (fb == NULL) {
fprintf(stderr, "error: out of memory\n");
exit(EXIT_FAILURE);
}
fb->numPeople = 0;
fb->capacity = DEFAULT_CAPACITY;
fb->names = calloc(fb->capacity, sizeof(char *));
if (fb->names == NULL) {
fprintf(stderr, "error: out of memory\n");
exit(EXIT_FAILURE);
}
fb->nameToId = MapNew();
fb->adj = calloc(fb->capacity, sizeof(struct adjNode *));
if (fb->adj == NULL) {
fprintf(stderr, "error: out of memory\n");
exit(EXIT_FAILURE);
}
return fb;
}
void FbFree(Fb fb) {
for (int i = 0; i < fb->capacity; i++) {
freeAdjList(fb->adj[i]);
}
free(fb->adj);
MapFree(fb->nameToId);
for (int i = 0; i < fb->numPeople; i++) {
free(fb->names[i]);
}
free(fb->names);
free(fb);
}
int FbNumPeople(Fb fb) {
return fb->numPeople;
}
bool FbAddPerson(Fb fb, char *name) {
if (fb->numPeople == fb->capacity) {
increaseCapacity(fb);
}
if (!MapContains(fb->nameToId, name)) {
int id = fb->numPeople++;
fb->names[id] = strdup(name);
MapSet(fb->nameToId, name, id);
return true;
} else {
return false;
}
}
static void increaseCapacity(Fb fb) {
int newCapacity = fb->capacity * 2;
fb->names = realloc(fb->names, newCapacity * sizeof(char *));
if (fb->names == NULL) {
fprintf(stderr, "error: out of memory\n");
exit(EXIT_FAILURE);
}
for (int i = fb->capacity; i < newCapacity; i++) {
fb->names[i] = NULL;
}
fb->adj = realloc(fb->adj, newCapacity * sizeof(struct adjNode));
if (fb->adj == NULL) {
fprintf(stderr, "error: out of memory\n");
exit(EXIT_FAILURE);
}
for (int i = fb->capacity; i < newCapacity; i++) {
fb->adj[i] = NULL;
}
fb->capacity = newCapacity;
}
bool FbHasPerson(Fb fb, char *name) {
return MapContains(fb->nameToId, name);
}
List FbGetPeople(Fb fb) {
List l = ListNew();
for (int id = 0; id < fb->numPeople; id++) {
ListAppend(l, fb->names[id]);
}
return l;
}
bool FbIsFriend(Fb fb, char *name1, char *name2) {
int id1 = nameToId(fb, name1);
int id2 = nameToId(fb, name2);
return inAdjList(fb->adj[id1], id2);
}
////////////////////////////////////////////////////////////////////////
// Your tasks
bool FbFriend(Fb fb, char *name1, char *name2) {
// TODO: Complete this function
return false;
}
int FbNumFriends(Fb fb, char *name) {
// TODO: Complete this function
return -1;
}
List FbMutualFriends(Fb fb, char *name1, char *name2) {
// TODO: Complete this function
List l = ListNew();
return l;
}
bool FbUnfriend(Fb fb, char *name1, char *name2) {
// TODO: Complete this function
return false;
}
int FbFriendRecs1(Fb fb, char *name, struct recommendation recs[]) {
// TODO: Complete this function
return 0;
}
////////////////////////////////////////////////////////////////////////
// Optional task
List FbFriendRecs2(Fb fb, char *name) {
// TODO: Complete this function
List l = ListNew();
return l;
}
////////////////////////////////////////////////////////////////////////
// Helper Functions
// Converts a name to an ID. Raises an error if the name doesn't exist.
static int nameToId(Fb fb, char *name) {
if (!MapContains(fb->nameToId, name)) {
fprintf(stderr, "error: person '%s' does not exist!\n", name);
exit(EXIT_FAILURE);
}
return MapGet(fb->nameToId, name);
}
static struct adjNode *newAdjNode(int v) {
struct adjNode *n = malloc(sizeof(*n));
if (n == NULL) {
fprintf(stderr, "error: out of memory\n");
exit(EXIT_FAILURE);
}
n->v = v;
n->next = NULL;
return n;
}
static bool inAdjList(struct adjNode *l, int v) {
for (struct adjNode *n = l; n != NULL && n->v <= v; n = n->next) {
if (n->v == v) {
return true;
}
}
return false;
}
static void freeAdjList(struct adjNode *l) {
struct adjNode *n = l;
while (n != NULL) {
struct adjNode *temp = n;
n = n->next;
free(temp);
}
}