Created
July 22, 2018 20:43
-
-
Save donpandix/7a6e6819bc84da6c3eb84632af6aa0b6 to your computer and use it in GitHub Desktop.
Lista Doblemente Enlazada con JAVA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.ThreadLocalRandom; | |
class Helper { | |
static int[] lista_generica_valores ( int largo_cadena ) { | |
int [] arreglo_base = new int[largo_cadena]; | |
for ( int n =0; n<largo_cadena;n++ ) { | |
arreglo_base[n] = ThreadLocalRandom.current().nextInt(0, 100000 + 1); | |
} | |
return arreglo_base; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListaEnlazadaDoble { | |
private NodoDoble ini; | |
private NodoDoble fin; | |
public ListaEnlazadaDoble (int[] lista) { | |
System.out.println("Lista doblemente enlazada"); | |
System.out.println("========================="); | |
this.ini = null; | |
this.fin = null; | |
for (int n : lista ) { | |
NodoDoble nuevoNodo = new NodoDoble( n ); | |
if (this.ini == null ) { | |
this.ini = nuevoNodo; | |
this.fin = nuevoNodo; | |
} else { | |
nuevoNodo.setAnterior(this.fin); | |
this.fin.setSiguiente( nuevoNodo ); | |
this.fin = nuevoNodo; | |
} | |
} | |
} | |
public void retornaLista () { | |
System.out.println("Recorre lista adelante"); | |
NodoDoble stepNodo = this.ini; | |
while ( stepNodo != null ) { | |
System.out.println(stepNodo.getValor() ); | |
stepNodo = stepNodo.getSiguiente(); | |
} | |
} | |
public void retornaListaReversa () { | |
System.out.println("Recorre lista reversa"); | |
NodoDoble stepNodo = this.fin; | |
while ( stepNodo != null ) { | |
System.out.println(stepNodo.getValor() ); | |
stepNodo = stepNodo.getAnterior(); | |
} | |
} | |
} | |
class NodoDoble { | |
private int valor; | |
private NodoDoble siguiente; | |
private NodoDoble anterior; | |
public NodoDoble ( int valor ) { | |
this.valor = valor; | |
} | |
public int getValor () { | |
return this.valor; | |
} | |
public void setSiguiente ( NodoDoble next ) { | |
this.siguiente = next; | |
} | |
public void setAnterior (NodoDoble prev) { | |
this.anterior = prev; | |
} | |
public NodoDoble getSiguiente () { | |
return this.siguiente; | |
} | |
public NodoDoble getAnterior () { | |
return this.anterior; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Main { | |
public static void main(String[] args) { | |
// Arreglo generico | |
int[] arreglo_base = Helper.lista_generica_valores(10); | |
// Ejecución lista doblemente enlazada | |
ListaEnlazadaDoble led = new ListaEnlazadaDoble(arreglo_base); | |
led.retornaLista(); | |
led.retornaListaReversa(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment