How can I get them to be in order?

3

I have a double linked list and I want them to be entered in order by date, but I do not see what the error is:

void AgrearE(NodoE **inicioptr,char id[5],char nombre[20],char desc[50],int dia,int mes,int anio)
    {
        NodoE *Nuevoptr,*aux ;
        Nuevoptr=crear_e(id,nombre,desc,dia,mes,anio);
        aux=*inicioptr;
        if(*inicioptr!=NULL)
        {
            if(aux->Sigptr==*inicioptr)
            {
                if(aux->fe.Mes>mes)
                {
                    Nuevoptr->Antptr=aux->Sigptr;
                    aux->Sigptr=Nuevoptr;
                    Nuevoptr->Sigptr=aux;
                    aux->Antptr=Nuevoptr;
                }
                else
                {
                    if(aux->fe.Mes<mes)
                    {
                        Nuevoptr->Sigptr=aux->Antptr;
                        aux->Antptr=Nuevoptr;
                        Nuevoptr->Antptr=aux->Sigptr;
                        aux->Sigptr=Nuevoptr;
                        *inicioptr=Nuevoptr;
                    }
                    else
                    {
                        if(aux->fe.Mes==mes)
                        {
                            if(aux->fe.Dia<dia)
                            {
                                Nuevoptr->Antptr=aux->Sigptr;
                                aux->Sigptr=Nuevoptr;
                                Nuevoptr->Sigptr=aux;
                                aux->Antptr=Nuevoptr;
                            }
                            else
                            {
                                Nuevoptr->Sigptr=aux->Antptr;
                                aux->Antptr=Nuevoptr;
                                Nuevoptr->Antptr=aux->Sigptr;
                                aux->Sigptr=Nuevoptr;
                                *inicioptr=Nuevoptr;
                            }
                        }
                    }

                }

            }
            else
            {
                while(aux->Sigptr!=*inicioptr && aux->fe.Mes<mes)
                {
                    if(aux->Sigptr->fe.Mes<mes)
                    {
                        aux=aux->Sigptr;
                    }
                    else
                    {
                        break;
                    }
                }
                if(mes>aux->fe.Mes && aux->Sigptr!=*inicioptr)
                {
                    aux->Antptr->Sigptr=Nuevoptr;
                    Nuevoptr->Antptr=aux->Antptr;
                    Nuevoptr->Sigptr=aux;
                    aux->Antptr=Nuevoptr;
                    *inicioptr=Nuevoptr;
                }
                else
                {
                    if(mes<aux->fe.Mes)
                    {
                        Nuevoptr->Antptr=aux;
                        Nuevoptr->Sigptr=aux->Sigptr;
                        aux->Sigptr->Antptr=Nuevoptr;
                        aux->Sigptr=Nuevoptr;
                    }
                    else
                    {
                        if(mes==aux->fe.Mes)
                        {
                            if(dia>aux->fe.Dia)
                            {
                                aux->Antptr->Sigptr=Nuevoptr;
                                Nuevoptr->Antptr=aux->Antptr;
                                Nuevoptr->Sigptr=aux;
                                aux->Antptr=Nuevoptr;
                                *inicioptr=Nuevoptr;
                            }
                            else
                            {
                                Nuevoptr->Antptr=aux;
                                Nuevoptr->Sigptr=aux->Sigptr;
                                aux->Sigptr->Antptr=Nuevoptr;
                                aux->Sigptr=Nuevoptr;
                            }
                        }
                    }
                }

            }
        }
        else
        {
            *inicioptr=Nuevoptr;
        }
    }
    
asked by Diana Lizeth 18.07.2017 в 22:19
source

1 answer

4

Insert neatly.

The process of insertion ordered in lists usually divides in:

  • Locate the insertion point : traverse the nodes from your root to the last node (top) by comparing the input value with the stored one; At the moment when the entry value is lower than the one stored (or you have reached the top) you will have found the insertion point.
  • Create the new node : you must link it to the node of the insertion point (the first that contains a value greater than the input or the top) and the next (if there is one).
  • Re-link the nodes : the node before the insertion point must point to the newly created node.
  • Expressed with a simpler code than yours would be as follows:

    Nodo *anterior = inicio;
    Nodo *punto_insercion = anterior->siguiente;
    
    while (punto_insercion  && (punto_insercion->dato > dato))
    {
        anterior = punto_insercion;
        punto_insercion = punto_insercion->siguiente;
    }
    
    // Una vez superado el bucle, punto_insercion apunta
    // al nodo inmediatamente superior al actual o al tope
    
    Nodo *nuevo_nodo = new Nodo(dato, punto_insercion);
    punto_insercion->anterior->siguiente = nuevo_nodo;
    nuevo_nodo->siguiente = punto_insercion;
    punto_insercion->anterior = nuevo_nodo;
    

    In the example code above I am assuming:

    • The list is not empty.
    • Nodo has a constructor that receives dato and the node to copy links from.
    • The dato to compare has a greater operator-than > .

    Broadly speaking, the code I have exposed would behave in the following way, with the following initial state: We want to insert the data k , we know that this data is greater than i and less than or , therefore in the loop:

  • First round, to > k? False, we continue.
  • Second round, e > k? False, we continue.
  • Third round, i > k? False, we continue.
  • Fourth round, or > k? True, or is the insertion point: Therefore, we create the node with the data k by copying the links of the node or , so that the next one of k will be u and its previous i : After this step, we only have to re-link the nodes, they are three affected nodes:

    • The siguiente of i , which corresponds to punto_insercion->anterior->siguiente , must be k .
    • The siguiente of k , which corresponds to nuevo_nodo->siguiente , must be or .
    • The anterior of or , which corresponds to punto_insercion->anterior , must be k .
  •     
    answered by 19.07.2017 / 10:23
    source