I have a c++ program in which I want to handle the InsertSort method of sorting .
I ask for the data with a case in the main.cpp file so that later in the Lista.cpp file I can indicate where to insert it, in this case I have insertaInicio.
case 1:
cout << "Codigo: ";
cin >> codigoDistribuidora;
nuevo.guardaNombre(codigoDistribuidora);
cout << "Nombre: ";
cin.ignore();
getline(cin, nombreDistribuidora);
nuevo.guardaNombre(nombreDistribuidora);
cout << "Domicilio: ";
getline(cin, domicilioDistribuidora);
nuevo.guardaNombre(domicilioDistribuidora);
cout << "Telefono: ";
cin >> telefonoDistribuidora;
nuevo.guardaNombre(telefonoDistribuidora);
cout << "Nombre del gerente: ";
cin.ignore();
getline(cin, nombreGerente);
nuevo.guardaNombre(nombreGerente);
l.insertaInicio(nuevo);
break;
and the start insert is this:
void Lista::insertaInicio(Distribuidora dato) {
Nodo *tmp = new Nodo;
Nodo *aux = inicio;
tmp->guardaObjeto(dato);
tmp->guardaNodoSig(NULL);
bool nada = vacia();
if (nada) {
inicio = tmp;
}
else {
tmp->sig = aux;
inicio = tmp;
}
string nombreDistribuidora = dato.damenombreDistribuidora();
string codigoDistribuidora = dato.damecodigoDistribuidora();
cout << "Has agregado la Distruibuidora con el nombre: '" << nombreDistribuidora << "' y codigo '" << codigoDistribuidora << "'" << endl;
}
How would it be a way to use InsertSort and order them when you insert them. I hope you understand me, thank you.
The above code should go in a separate function. Apart from greatly improving the readability of the code, it should be borne in mind that the statements
switch
can be quite dangerous if one disappearsbreak
or if it sneaks in early. It is highly recommended to havecase
as simple as possible.On the other hand, and since you don't include the full implementation of
Lista
, I'll tell you thatinsertaInicio
you may have a problem with the function:What if the list is empty and therefore
inicio
points tonullptr
? Since you are making a copy ofinicio
en ,aux
it is not worth modifying the address pointed byaux
, since in that caseinicio
it will not find out.And well, speaking of your problem, what you have to do is find where in the list you have to insert the new element. This way the list will ALWAYS be sorted. What you need is to determine how to order the list:
If we assume that you choose the first option of the algorithm and that the order is increasing, to determine the position of the new element it would be the following:
inicio
it isnullptr
the list is empty, then itinicio
points to the new element and we are doneSaid with code it could be something like this:
All the best.
It appears that you are working with a single linked list which makes the sorted insertion process slightly more complicated.
In this type of lists, the ordered insertion process is divided into:
In the example code above I am assuming:
Nodo
has a constructor to receivedato
and a node to point to .dato
to compare has a greater-than operator>
.Keep in mind that what I have written is a guide, you must adapt it to your needs and keep in mind that I have not tested the pseudo-code