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;
}
}
}
}