Infinite Loop in java

1

I have a problem with a code that should order me certain orders according to the delivery date more next until the latest, the code is implemented in a list, which contains all the orders that were received after the date that the user delivers and those are those that must be reordered, it turns out that when executing the method the method is traversed without stopping in the line "while (aux2.getNext ()! = null) {" since, I do not know why they are created infinity aux2 so the list never gets to null therefore never leaves the while mentioned, helpaaaa

 public void OrdenarListaEDD(Date fechaUs){
NodoLista aux=head;
 NodoLista aux2=head.getNext() ; 

      while(aux.getNext()!=null){
          aux2=head.getNext();
          while(aux2.getNext()!=null ){
           if(!aux2.getNext().getDato().getFec_ent().before(aux.getNext().getDato().getFec_ent())){
           } else {
               NodoLista aux3=aux2.getNext();
               aux3.setNext(aux.getNext());
               aux.setNext(aux3);
               aux2.setNext(aux2.getNext().getNext());
              }
           aux2=aux2.getNext();

         }

          aux=aux.getNext();
}

} 
    
asked by sebastian 25.10.2016 в 05:14
source

1 answer

1

Without analyzing if the algorithm will give you the result you are looking for, your mistake is that, unwittingly, you create a circular list:

Let's see if I explain, suppose you have these nodes in your list (I use an index to identify each node and the notation -> to indicate the index of the next node in the list.

1. 1/1/2000 ->2
2. 1/1/2002 ->3
3. 1/1/2001 ->4
4. 1/1/2004

After initializing, we would be like this:

1. 1/1/2000 -> 2    <- aux
2. 1/1/2002 -> 3    <- aux2
3. 1/1/2001 -> 4
4. 1/1/2004

the if

if(!aux2.getNext().getDato().getFec_ent().before(aux.getNext().getDato().getFec_ent()))

is not fulfilled and we enter the else:

There, this is happening:

           NodoLista aux3=aux2.getNext();

1. 1/1/2000 -> 2    <- aux
2. 1/1/2002 -> 3    <- aux2
3. 1/1/2001 -> 4    <- aux3
4. 1/1/2004

           aux3.setNext(aux.getNext());

1. 1/1/2000 -> 2    <- aux
2. 1/1/2002 -> 3    <- aux2
3. 1/1/2001 -> 2    <- aux3
4. 1/1/2004

           aux.setNext(aux3);

1. 1/1/2000 -> 3    <- aux
2. 1/1/2002 -> 3    <- aux2
3. 1/1/2001 -> 2    <- aux3
4. 1/1/2004

           aux2.setNext(aux2.getNext().getNext());

1. 1/1/2000 -> 3    <- aux
2. 1/1/2002 -> 2    <- aux2
3. 1/1/2001 -> 2    <- aux3
4. 1/1/2004

As you see, now, the next node of index 2, is the same node with index 2, while no node is already related to index node 4. Your logic in principle seems correct, but by doing% obvious .getNext().getNext() the fact that you have previously reassigned the next node of the next node.

You can, for example, use another variable to store it previously, change the else code for something like:

           NodoLista aux3=aux2.getNext();
           NodoLista aux4=aux3.getNext();
           aux3.setNext(aux.getNext());
           aux.setNext(aux3);
           aux2.setNext(aux4);

This will no longer create the circular list, nor break the relationship with the next node in the list.

    
answered by 25.10.2016 / 07:08
source