Why does deleting a user then adding a user not update properly on friend's list
#include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

/*
 * Check if two users are friends
 *
 * Return:
 *    - 0 if the users are friends
 *    - 1 if the users are not friends
*/
int check_friends(const User *user1, User *user2) {
    for (int i = 0; i < MAX_FRIENDS; i++) {
        if(user1 -> friends[i] != NULL) {
            if (strcmp(user1 -> friends[i] -> name, user2 -> name) == 0) {
                return 0;
            }
        }
    }
    return 1;
}

/*
 * Create a new user with the given name.  Insert it at the tail of the list
 * of users whose head is pointed to by *user_ptr_add.
 *
 * Return:
 *   - 0 if successful
 *   - 1 if a user by this name already exists in this list
 *   - 2 if the given name cannot fit in the 'name' array
 *       (don't forget about the null terminator)
 */
int create_user(const char *name, User **user_ptr_add) {
    // Check length of name
    if (strlen(name) > MAX_NAME - 1) {
        return 2;
    }
    
    // Create Space for the user & Initialize Variables
    User *new_user = malloc(sizeof(User));
    strcpy(new_user -> name, name);
    strcpy(new_user -> profile_pic, "");
    new_user -> first_post = NULL;
    for (int i = 0; i < MAX_FRIENDS; i++) {
        new_user -> friends[i] = NULL;
    }
    new_user -> next = NULL;

 
    // First User to be created
    if (*user_ptr_add == NULL) {
        *user_ptr_add = new_user;
        return 0;
    }

    // User(s) already exist
    User *curr = *user_ptr_add;

    // printf("Linked List Excluding Last: ");

    while (curr -> next != NULL) {
        //printf("%s ", curr -> name);
        if (strcmp(curr -> name, name) == 0) {
            return 1;
        }
        curr = curr -> next;
    }

    // Check for the final node
    if (strcmp(curr -> name, name) == 0) {
        return 1;
    }

    curr -> next = new_user;

    //printf("\n%s %s\n", curr -> name, new_user -> name);
    
    return 0;
}


/*
 * Return a pointer to the user with this name in
 * the list starting with head. Return NULL if no such user exists.
 *
 * NOTE: You'll likely need to cast a (const User *) to a (User *)
 * to satisfy the prototype without warnings.
 */
User *find_user(const char *name, const User *head) {
    // Check to make sure at least one User exists
    if ((User *) head == NULL) {
        return NULL;
    } 

    // Find a User with the same name, and return if it exists
    User *curr = (User *) head;
    while (curr != NULL) {
        if (strcmp(curr -> name, name) == 0) {
            return curr;
        }

        curr = curr -> next;
    }

    return NULL;
}


/*
 * Print the usernames of all users in the list starting at curr.
 * Names should be printed to standard output, one per line.
 */
void list_users(const User *curr) {
    // Check to make sure at least one User exists
    if ((User *) curr == NULL) {
    } else {
        User *current = (User *) curr;
        
        // Loop through every User
        while (current != NULL) {
            printf("User: %s\n", current -> name);
            printf("Profile Pic: %s\n", current -> profile_pic);
            printf("Friends: ");
            for (int i = 0; i < MAX_FRIENDS; i++) {
                if (current -> friends[i] != NULL) {
                    printf("%s ", current -> friends[i] -> name);
                }
            }
            printf("\n\n");
            current = current -> next;
        }
    }
}


/*
 * Change the filename for the profile pic of the given user.
 *
 * Return:
 *   - 0 on success.
 *   - 1 if the file does not exist or cannot be opened.
 *   - 2 if the filename is too long.
 */
int update_pic(User *user, const char *filename) {
    // Check filename length
    if (strlen(filename) > MAX_NAME - 1) {
        return 2;
    }

    // Check file existance and workings
    FILE *pic = fopen(filename, "r");
    if (pic == NULL) {
        return 1;
    }
    fclose(pic);

    // Change user profile picture
    strncpy(user -> profile_pic, filename, MAX_NAME - 1);
    return 0;
}


/*
 * Make two users friends with each other.  This is symmetric - a pointer to
 * each user must be stored in the 'friends' array of the other.
 *
 * New friends must be added in the first empty spot in the 'friends' array.
 *
 * Return:
 *   - 0 on success.
 *   - 1 if the two users are already friends.
 *   - 2 if the users are not already friends, but at least one already has
 *     MAX_FRIENDS friends.
 *   - 3 if the same user is passed in twice.
 *   - 4 if at least one user does not exist.
 *
 * Do not modify either user if the result is a failure.
 * NOTE: If multiple errors apply, return the *largest* error code that applies.
 */
int make_friends(const char *name1, const char *name2, User *head) {
    User *person1 = find_user(name1, head);
    User *person2 = find_user(name2, head);

    // Check to make sure both users exist
    if (person1 == NULL || person2 == NULL) {
        return 4;
    }

    // Check for the same user passed in twice
    if (strcmp(name1, name2) == 0) {
        return 3;
    }

    int open1 = MAX_FRIENDS;
    int open2 = MAX_FRIENDS;
    for (int i = MAX_FRIENDS - 1; i >= 0; i--) {
        // Check to see if users are already friends
        if(person1 -> friends[i] != NULL) {
            if (strcmp(person1 -> friends[i] -> name, name2) == 0) {
                return 1;
            }
        }

        // Find first open index of each user's friend list
        if (person1 -> friends[i] == NULL) {
            open1 = i;
        }
        if (person2 -> friends[i] == NULL) {
            open2 = i;
        }

        // Check to ensure there is room in both user's friend lists
        if (open1 == MAX_FRIENDS || open2 == MAX_FRIENDS) {
            return 2;
        }
    }

    // Add to list
    person1 -> friends[open1] = person2;
    person2 -> friends[open2] = person1;
    return 0;
}


/*
 * Print a user profile.
 * For an example of the required output format, see the example output
 * linked from the handout.
 * Return:
 *   - 0 on success.
 *   - 1 if the user is NULL.
 */
int print_user(const User *user) {
    if (user == NULL) {
        return 1;
    }

    // Print Profile Picture
    FILE *pic = fopen(user -> profile_pic, "r");
    if (pic == NULL) {
    } else {
        char line_info[100];
        while(fgets(line_info, 100, pic) != NULL) {
            printf("%s\n", line_info);
        }
        fclose(pic);
    }

    // Print Name
    printf("Name: %s\n", user -> name);
    printf("------------------------------------------\n");

    // Print Friends List
    printf("Friends:\n");
    for (int i = 0; i < MAX_FRIENDS; i++) {
        if (user -> friends[i] != NULL) {
            printf("%s\n", user -> friends[i] -> name);
        }
    }
    printf("------------------------------------------\n");

    // Print Posts
    printf("Posts:\n");
    struct post *curr;
    if (user -> first_post != NULL) {
        curr = user -> first_post;
    }

    while (curr != NULL) {
        printf("From: %s\n", curr -> author);
        printf("Date: %s\n", ctime(curr -> date));
        printf("%s\n", curr -> contents);

        curr = curr -> next;

        if (curr != NULL) {
            printf("\n===\n\n");
        }
    }

    printf("------------------------------------------\n");

    return 0;
}


/*
 * Make a new post from 'author' to the 'target' user,
 * containing the given contents, IF the users are friends.
 *
 * Insert the new post at the *front* of the user's list of posts.
 *
 * 'contents' is a pointer to heap-allocated memory - you do not need
 * to allocate more memory to store the contents of the post.
 *
 * Return:
 *   - 0 on success
 *   - 1 if users exist but are not friends
 *   - 2 if either User pointer is NULL
 */
int make_post(const User *author, User *target, char *contents) {
    // Check both exists
    if (author == NULL || target == NULL) {
        return 2;
    }
    
    // Check users are friends
    int friend = check_friends(author, target);
    if (friend == 1) {
        return 1;
    }

    // Create Post
    Post *new_post = malloc(sizeof(Post));
    strcpy(new_post -> author, author -> name);
    time_t *curtime = malloc(sizeof(time_t));
    time(curtime);
    new_post -> date = curtime;
    new_post -> contents = contents;

    // Link Post to Post Linked List
    if (target -> first_post == NULL) {
        target -> first_post = new_post;
    } else {
        new_post -> next = target ->first_post;
        target -> first_post = new_post;
    }

    return 0;
}


/*
 * 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) {
    // Check to ensure user with name exists
    if (*user_ptr_del == NULL) {
        return 1;
    }

    // Check to see if first user is to be deleted 
    User *user = find_user(name, *user_ptr_del);
    if (user == NULL) {
        return 1;
    }

    User *prev = NULL;
    User *curr = *user_ptr_del;

    if (strcmp((*user_ptr_del) -> name, name) == 0) { // Check to see if first user is to be deleted
    } else { // Check the rest of the users
        while (prev == NULL) {
            if (strcmp((curr -> next) -> name, name) == 0) {
                prev = curr;
            }
            curr = curr -> next;
        }
    }

    // Delete user from friend's list 
    for (int i = 0; i < MAX_FRIENDS; i++) {
        User *friend = NULL;
        if (user -> friends[i] != NULL) {
            friend = user -> friends[i];

            for (int j = 0; j < MAX_FRIENDS; j++) {
                if (friend -> friends[i] == user) {
                    friend -> friends[i] = NULL;
                }
            }
        }
    }

    // Free all dynamically allocated memory
    Post *curr_post = user -> first_post;
    while (curr_post != NULL) {
        Post *temp = curr_post;
        curr_post = curr_post -> next;
        free(temp -> contents);
        free(temp -> date);
        free(temp);
    }

    if (prev == NULL) {
        *user_ptr_del = curr -> next;
    } else {
        prev -> next = curr -> next;
    }
    free(curr);

    return 0;
}
