
/*
 * From the list pointed to by *user_ptr_del, delete the user
 * with the given name.
 * Remove the deleted user from any lists of friends.
 *
 * Return:
 *   - 0 on success.
 *   - 1 if a user with this name does not exist.
 */
int delete_user(const char *name, User **user_ptr_del) {
    // initialize variables for linked list traversal
    User *current = *user_ptr_del;
    User *prev = NULL;
    
    // search for the user to delete
    while (current != NULL && strcmp(current->name, name) != 0) {
        prev = current;
        current = current->next;
    }
    
    // if user was not found
    if (current == NULL) {
        return 1;
    }
                                  
    // Remove user from every friend list
    int i, j;
    for (i = 0; i < MAX_FRIENDS; i++) {
        if (current->friends[i] == NULL) {
            break;
        }
        for (j = 0; j < MAX_FRIENDS; j++) {
            if (current->friends[i]->friends[j] == current) {
                current->friends[i]->friends[j] = NULL;
                break;
            }
        }
    }

    // remove user from list
    if (prev == NULL) {
        *user_ptr_del = current->next;
    } else {
        prev->next = current->next;
    }

    // free dynamically allocated memory for the user's posts
    Post *post = current->first_post;
    while (post != NULL) {
        Post *temp = post;  // use a temporary variable
        post = post->next;
        free(temp->contents);
        free(temp);
    }

    free(current);  // free the user's allocated memory
    
    return 0;  // return 0 on success
}

frees all allocated memory