Problem with handling lists in JAVA

0

I have a problem with handling lists in Java

I do not know how to add elements to a certain list, for example:

{2,4,5,7,9}

So that I get the intermediate numbers in the same list, that is, modifying the main one, being as follows:

{2,3,4,5,6,7,8,9}

This is the code:

public static void main(String[] args) {
    Lista lista = new Lista();
    lista.insertarOrden(2);
    lista.insertarOrden(4);
    lista.insertarOrden(5);
    lista.insertarOrden(7);
    lista.insertarOrden(9);
    System.out.println(lista);
}

public class Lista {  
    public class Nodo {
        int info;
        private Nodo sig;
        public Nodo (int info) {
           this.info = info;
           sig = null;
        }
}

private Nodo primero;
    public void fill () {
       Nodo aux = primero;
       while (aux != null) {

    }
}

public void insertarOrden (int valor) {
    if (primero == null) {
        primero = new Lista.Nodo(valor);
    }else {
        if (primero.info > valor) {
            Lista.Nodo aux = new Lista.Nodo(valor);
            aux.sig = primero;
            primero = aux;
        }else {
            Lista.Nodo aux = primero;
            while (aux.sig != null && aux.sig.info < valor) {
                aux = aux.sig;
            }

            Lista.Nodo nuevo = new Lista.Nodo(valor);
            nuevo.sig = aux.sig;
            aux.sig = nuevo;
        }
    }
    }
}
    
asked by Daniel 02.05.2017 в 19:18
source

2 answers

0

One solution would be to have a variable that has the value of the last element, and then with a for insert the remaining elements, additionally I added a imprimir method for the linked list.

 public void fill () {
    Nodo aux = primero;
    int  ultimo=0;/* Varible Ultimo Inicializada en 0*/
    while (aux != null) {
     /* Si el valor del Nodo es Mayor que ultimo */
        if(aux.info>ultimo){
           /* Iteramos desde ultimo +1 hasta el siguiente valor del Siguiente Nodo
            y ese valor de i , le añadimos a la Lista*/
            for (int i = ultimo+1; i < aux.info; i++) {
                insertarOrden(i);
            }
            ultimo=aux.info; /* Asignamos a Ultimo el valor de Nodo*/
        }
        aux = aux.sig;
    }
}

public void Imprimir(){
    Nodo aux = primero;
    while (aux != null) {
        System.out.println(aux.info);
        aux=aux.sig;
    }
}


/* Explicación por Iteración  lista = (2,4,5,7,9)', ultimo =0
1 Iteración :
    Valor de Aux.info = 2 -> if(2>0)==  TRUE
    For : Desde i=(0+1) hasta Aux.info =2 {
       Insertamos(1);
    }
    ultimo = aux.info =2;
2 Iteración :
    Valor de Aux.info = 4 -> if(4>2)==  TRUE
    For : Desde i=(2+1) hasta Aux.info =4 {
       Insertamos(3);
    }
    ultimo = aux.info =4;
3 Iteración :
    Valor de Aux.info = 5 -> if(5>4)==  TRUE
    For : Desde i=(4+1) hasta Aux.info =5 {
       No se Inserta Nada
    }
    ultimo = aux.info =5;
4 Iteración :
    Valor de Aux.info = 7 -> if(7>5)==  TRUE
    For : Desde i=(5+1) hasta Aux.info =7 {
       Insertamos(6)
    }
    ultimo = aux.info =7;
5 Iteración :
    Valor de Aux.info = 9 -> if(9>7)==  TRUE
    For : Desde i=(7+1) hasta Aux.info =9 {
       Insertamos(8)
    }
    ultimo = aux.info =9;
*/
    
answered by 02.05.2017 / 20:05
source
-1

You should put the code you have to be more precise, but I'll give you an example:

With the Collections class and the sort method as follows:

Collections.sort(listaNumeros);

So your complete example would be:

import java.util.ArrayList;
import java.util.Collections;

public class ListarNumeros
{
  public static void main(String[] args)
  {
    ArrayList<Integer> lista = new ArrayList<Integer>();
    lista.add(8); 
    lista.add(6); 
    lista.add(3);

    Collections.sort(lista);

    for (Integer numero: lista) {
        System.out.println(numero);
    }   

  }
}

Result:

3 6 8

    
answered by 02.05.2017 в 19:40