/* Remove the user with matching user and group name and * remove all her transactions from the transaction list. * Return 0 on success, and -1 if no matching user exists. * Remember to free memory no longer needed. * (Wait on implementing the removal of the user's transactions until you * get to Part III below, when you will implement transactions.) */ int remove_user(Group *group, const char *user_name) { /* Gets the previous user before the user to be deleted if the to be deleted * user is not a head. If the to be deleted user is a head, the to be deleted * is returned. If the user doesn't exist in the group, NULL is returned. */ User *prev_user = find_prev_user(group, user_name); /* If the user returned is not NULL, this means that user exists in the group, * therefore we check if the returned user is a head or the previous user. * 1) If it is a previous user, we set the previous user next pointer to * the next next user and free space for the deleted user. */ if (prev_user != NULL) { if (strcmp(prev_user->name, user_name) == 0) { group->users = prev_user->next; remove_xct(group, user_name); free_dp(prev_user->name); free_dp(prev_user); return 0; /* 2) If it is a head node, we reset the head to the next user and free * up space for the deleted node. Free space for the user name * and the user, also points the user to NULL to avoid dangling * pointers. */ } else if (strcmp(prev_user->next->name, user_name) == 0) { User *temp = prev_user->next; prev_user->next = prev_user->next->next; remove_xct(group, user_name); free_dp(temp->name); free_dp(temp); } // Return 0 if we the deletion is successful. return 0; /* If the user returned is NULL, that means the user name doesn't exist, no * users are deleted, and -1 is returned. */ } else { return -1; } }
/* Remove the user with matching user and group name and * remove all her transactions from the transaction list. * Return 0 on success, and -1 if no matching user exists. * Remember to free memory no longer needed. * (Wait on implementing the removal of the user's transactions until you * get to Part III below, when you will implement transactions.) */ int remove_user(Group *group, const char *user_name) { User *ptr; ptr = group->users; while( ptr->next != NULL){ //If the next node is the node to be deleted if(strcmp((ptr->next)->name, user_name) == 0){ //Keep a pointer on the thing to delete so as to free up //memory later User *delete = ptr->next; ptr->next = (ptr->next)->next; //Remove the xcts remove_xct(group, user_name); free(delete); return 0; } else{