-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha2_functions.c
465 lines (393 loc) · 11.8 KB
/
a2_functions.c
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/********* definitions.c ********
Student Name = Kalid Wehbe
Student Number = 101259994
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "a2_nodes.h"
#include "a2_functions.h"
#include <stdbool.h>
// Your solution goes here
/*
Function that creates a new user and adds it to a sorted (ascending order) linked list at
the proper sorted location. Return the head of the list.
*/
user_t *add_user(user_t *users, const char *username, const char *password)
{
// Create a new user
user_t *new_user = (user_t *)malloc(sizeof(user_t));
if (new_user == NULL)
{
printf("malloc failed\n");
exit(EXIT_FAILURE);
}
// Initialize the new user
strncpy(new_user->username, username, sizeof(new_user->username));
strncpy(new_user->password, password, sizeof(new_user->password));
new_user->friends = NULL; // Initialize friends list to empty
new_user->posts = NULL; // Initialize posts list to empty
new_user->next = NULL; // Initialize next pointer to NULL
// If the list is empty or the new user should be inserted at the beginning
if (users == NULL || strcmp(username, users->username) < 0)
{
new_user->next = users;
return new_user;
}
// Find the proper location to insert the new user in ascending order
user_t *current = users;
while (current->next != NULL && strcmp(username, current->next->username) > 0)
{
current = current->next;
}
// Insert the new user at the proper location
new_user->next = current->next;
current->next = new_user;
return users;
}
/*
Function that searches if the user is available in the database
Return a pointer to the user if found and NULL if not found.
*/
user_t *find_user(user_t *users, const char *username)
{
user_t *current = users;
while (current != NULL)
{
if (strcmp(current->username, username) == 0)
{
// User found, return a pointer to the user
return current;
}
current = current->next;
}
// User not found, return NULL
return NULL;
}
/*
Function that creates a new friend's node.
Return the newly created node.
*/
friend_t *create_friend(const char *username)
{
friend_t *newFriend = (friend_t *)malloc(sizeof(friend_t));
if (newFriend == NULL)
{
printf("malloc failed\n");
exit(EXIT_FAILURE);
}
// Initialize the new friend
strncpy(newFriend->username, username, sizeof(newFriend->username));
newFriend->next = NULL;
return newFriend;
}
/*
Function that links a friend to a user. The friend's name should be added into
a sorted (ascending order) linked list.
*/
void add_friend(user_t *user, const char *friend)
{
friend_t *newFriend = create_friend(friend);
// If the list is empty or the new friend should be inserted at the beginning
if (user->friends == NULL || strcmp(friend, user->friends->username) < 0)
{
newFriend->next = user->friends;
user->friends = newFriend;
}
else
{
friend_t *currentFriend = user->friends;
friend_t *prevFriend = NULL;
// Find the proper location to insert the new friend in ascending order
while (currentFriend != NULL && strcmp(friend, currentFriend->username) > 0)
{
prevFriend = currentFriend;
currentFriend = currentFriend->next;
}
// Insert the new friend at the proper location
newFriend->next = currentFriend;
prevFriend->next = newFriend;
}
printf("Friend %s added successfully.\n", friend);
}
/*
Function that removes a friend from a user's friend list.
Return true of the friend was deleted and false otherwise.
*/
_Bool delete_friend(user_t *user, char *friend_name)
{
friend_t *currentFriend = user->friends;
friend_t *prevFriend = NULL;
// Find the friend with the specified name
while (currentFriend != NULL && strcmp(currentFriend->username, friend_name) != 0)
{
prevFriend = currentFriend;
currentFriend = currentFriend->next;
}
// If the friend is found, delete it
if (currentFriend != NULL)
{
// Update pointers to bypass the friend to be deleted
if (prevFriend != NULL)
{
prevFriend->next = currentFriend->next;
}
else
{
// If the friend is the first in the list, update the head of the list
user->friends = currentFriend->next;
}
free(currentFriend);
printf("Friend %s deleted successfully.\n", friend_name);
return true;
}
// Friend not found
printf("Error: Friend %s not found.\n", friend_name);
return false;
}
/*
Function that creates a new user's post.
Return the newly created post.
*/
post_t *create_post(const char *text)
{
post_t *new_post = (post_t *)malloc(sizeof(post_t));
if (new_post == NULL)
{
printf("malloc failed\n");
exit(EXIT_FAILURE);
}
// Initialize the new post
strncpy(new_post->content, text, sizeof(new_post->content));
new_post->next = NULL;
return new_post;
}
/*
Function that adds a post to a user's timeline. New posts should be added following
the stack convention (LIFO) (i.e., to the beginning of the Posts linked list).
*/
void add_post(user_t *user, const char *text)
{
post_t *new_post = create_post(text);
// Add the new post to the beginning of the user's posts list (LIFO)
new_post->next = user->posts;
user->posts = new_post;
}
/*
Function that removes a post from a user's list of posts.
Return true if the post was deleted and false otherwise.
*/
_Bool delete_post(user_t *user, int number)
{
if (user != NULL)
{
post_t *currentPost = user->posts;
post_t *prevPost = NULL;
int currentPosition = 1;
// Find the post at the specified position
while (currentPost != NULL && currentPosition < number)
{
prevPost = currentPost;
currentPost = currentPost->next;
currentPosition++;
}
// If the post is found, delete it
if (currentPost != NULL)
{
// Update pointers to bypass the post to be deleted
if (prevPost != NULL)
{
prevPost->next = currentPost->next;
}
else
{
// If the post is the first in the list, update the head of the list
user->posts = currentPost->next;
}
free(currentPost);
return true;
}
}
// Post not found or user is NULL
return false;
}
/*
Function that displays a specific user's posts
*/
void display_user_posts(user_t *user)
{
if (user != NULL)
{
printf("-------------------------------------------\n");
printf("%s's posts:\n", user->username);
// Iterate through the user's posts and print each one
post_t *currentPost = user->posts;
int postNum = 1;
while (currentPost != NULL)
{
printf("%d- %s\n", postNum, currentPost->content);
currentPost = currentPost->next;
postNum++;
}
// Check if there are no posts
if (user->posts == NULL)
{
printf("No posts available for user %s\n", user->username);
}
printf("-------------------------------------------\n");
}
else
{
printf("-------------------------------------\n");
printf(" User not found. \n");
printf("-------------------------------------\n");
}
}
/*
Function that displays a specific user's friends
*/
void display_user_friends(user_t *user)
{
friend_t *currentFriend = user->friends;
if (currentFriend == NULL)
{
printf("No friends available for %s.\n", user->username);
}
else
{
int friendNumber = 1;
printf("-----------------------------------\n");
printf("List of %s's friends\n", user->username);
// Display the list of user's friends
while (currentFriend != NULL)
{
printf("%d- %s\n", friendNumber++, currentFriend->username);
currentFriend = currentFriend->next;
}
}
}
/*
Function that displays all the posts of 2 users at a time from the database.
After displaying 2 users' posts, it prompts if you want to display
posts of the next 2 users.
If there are no more post or the user types “n” or “N”, the function returns.
*/
void display_all_posts(user_t *users)
{
int userCount = 0;
user_t *current = users;
while (current != NULL)
{
// Display the posts of the current user
display_user_posts(current);
userCount++;
// Move to the next user
current = current->next;
// If two users' posts have been displayed, prompt the user
if (userCount % 2 == 0)
{
char choice;
printf("\nDo you want to display posts of the next 2 users? (Y/N): ");
scanf(" %c", &choice);
if (choice == 'N' || choice == 'n')
{
printf("All posts have been displayed.\n");
return;
}
}
}
// If there are fewer than 2 users or the user chooses to display more posts after all have been displayed
printf("All posts have been displayed.\n");
}
/*
Fucntion that free all users from the database before quitting the application.
*/
void teardown(user_t *users)
{
user_t *current = users;
user_t *next;
while (current != NULL)
{
// Free friends linked list
friend_t *currentFriend = current->friends;
friend_t *nextFriend;
while (currentFriend != NULL)
{
nextFriend = currentFriend->next;
free(currentFriend);
currentFriend = nextFriend;
}
// Free posts linked list
post_t *currentPost = current->posts;
post_t *nextPost;
while (currentPost != NULL)
{
nextPost = currentPost->next;
free(currentPost);
currentPost = nextPost;
}
// Move to the next user and free the current user
next = current->next;
free(current);
current = next;
}
}
/*
Function that prints the main menu with a list of options for the user to choose from
*/
void print_menu()
{
printf("**********************************\n");
printf(" MAIN MENU: \n");
printf("**********************************\n");
printf("1. Register a new User\n");
printf("2. Manage a user's profile (change passwird)\n");
printf("3. Manage a user's posts (display/add/remove)\n");
printf("4. Manage a user's friends (display/add/remove)\n");
printf("5. Display All Posts\n");
printf("6. Exit\n");
printf("Enter your choice: ");
}
/*
******** DONT MODIFY THIS FUNCTION ********
Function that reads users from the text file.
IMPORTANT: This function shouldn't be modified and used as is
******** DONT MODIFY THIS FUNCTION ********
*/
user_t *read_CSV_and_create_users(FILE *file, int num_users)
{
user_t *users = NULL;
char buffer[500];
fgets(buffer, sizeof(buffer), file); // Read and discard the header line
int count = 0;
for (int i = 0; i < num_users; i++)
{
fgets(buffer, sizeof(buffer), file);
buffer[strcspn(buffer, "\r\n")] = 0; // Remove newline characters
char *token = strtok(buffer, ",");
char *token2 = strtok(NULL, ",");
users = add_user(users, token, token2);
char *username = token;
token = strtok(NULL, ",");
user_t *current_user = users;
for (; current_user != NULL && strcmp(current_user->username, username) != 0; current_user = current_user->next)
;
while (token != NULL && strcmp(token, ",") != 0 && count < 3)
{
if (strcmp(token, " ") != 0)
{
add_friend(current_user, token);
}
token = strtok(NULL, ",");
count++;
}
count = 0;
// token = strtok(NULL, ",");
while (token != NULL && strcmp(token, ",") != 0)
{
add_post(current_user, token);
token = strtok(NULL, ",");
}
}
return users;
}