I have a program that allows you to create users and make them friends with each other. There is a maximum number of friends allowed for each user. when i make two users friends and one of the friend's friendlist is now full, for some reason, in addition to the added friend being added to the friendlist, the first friend that was added to their friendlist is duplicated and added to the very end of the list. what is causing this?
int make_friends(const char *name1, const char *name2, User *head) {
    // Create users friend1 and friend2
    User *friend1 = find_user(name1, head);
    User *friend2 = find_user(name2, head);

    // Check for user non-existence
    if (friend1 == NULL || friend2 == NULL) {
        return 4;
    }

    // Check for same user passed twice
    if (friend1 == friend2) {
        return 3;
    }

    // Check for full friends list
    if (friend1->friends[MAX_FRIENDS - 1] != NULL || friend2->friends[MAX_FRIENDS - 1] != NULL) {
        return 2;
    }

    // Check for friend already exists
    if (check_if_not_friends(friend1, friend2) == 1) {
        return 1;
    }

    // Set the first NULL element to friend2
    for (int i = 0; i < MAX_FRIENDS; i++) {
        if (friend1->friends[i] == NULL) {
            friend1->friends[i] = friend2;
            i = MAX_FRIENDS;  // Update i to exit the loop
        }
    }

    // Set the first NULL element to friend1
    for (int i = 0; i < MAX_FRIENDS; i++) {
        if (friend2->friends[i] == NULL) {
            friend2->friends[i] = friend1;
            i = MAX_FRIENDS;  // Update i to exit the loop
        }
    }

    return 0;
}


