// Subfunction to find the right place for the newPerson and look out for duplicates Tree * findPlace(Tree * currentPerson, Tree * newPerson) { // if the newName is larger than the pivot if (strcmp(newPerson->name, currentPerson->name) > 0) { // the new name goes to the right side if (currentPerson->nextPerson != NULL) { // repeat until the right place has been found findPlace(currentPerson->nextPerson, newPerson); } else { currentPerson->nextPerson = newPerson; printf("Successfully added %s to the list\n", newPerson->name); } // if the new name is smaller than the pivot } else if (strcmp(newPerson->name, currentPerson->name) < 0) { // the new name goes to the left side if (currentPerson->previousPerson != NULL) { // repeat until the right place has been found findPlace(currentPerson->previousPerson, newPerson); } else { currentPerson->previousPerson = newPerson; printf("Successfully added %s to the list\n", newPerson->name); } // if the name already exists on the list } else if (strcmp(newPerson->name, currentPerson->name) == 0) { // when its not deleted, let user know of the duplicate if (currentPerson->deleted != 1) { printf("%s already exists in the list\n", newPerson->name); // undo deletion-> readding the person on the list } else { currentPerson->deleted = 0; printf("Successfully added %s to the list\n", newPerson->name); } } // return the item that got changed return currentPerson; }
// Main function for adding a new member to the list and finding a right place for it void addMember() { Tree * newPerson; newPerson = makeNewMember(); newPerson->deleted = 0; // if tree was previously empty, make the first item the pivot if (emptyTree == 0) { pivot = newPerson; emptyTree = 1; // if tree was not empty before, find the right place for the new item } else { findPlace(pivot, newPerson); } options(); }
void agregarPersona(Persona gente[]) { Persona *p = findPlace(gente); int retorno; if(p != NULL) { retorno = pedirString(p->nombre,"Ingrese Nombre:\n",30,3,"El nombre de tener entre 3 y 30 caracteres\n"); if(retorno != -1) retorno = pedirInt(&p->edad,"Ingrese edad:\n",120,1,"Ingrese una edad valida\n"); if(retorno != -1) retorno = pedirLong(&p->dni,"Ingrese DNI:\n",0,1,"El DNI debe ser valido\n"); if(retorno != -1) p->inUseFlag = 1; }else { printf("No hay mas espacio\n"); } }