// CLASE: Nodo
package listadoble1;
public class Nodo {
Nodo ant;
Object info;
Nodo sig;
Nodo(Object dato){
info = dato; ant
= null; sig =
null;
}
}
3) ListaDoble1: Clase ListaD
// CLASE: ListaD
package listadoble1;
public class ListaD {
Nodo Primero;
ListaD(){
Primero = null;
}
// ───────────────────────────────────────
// MÉTODO: PARA VER SI LA LISTA ESTA VACÍO
// ───────────────────────────────────────
public boolean estaVacio(){
if( Primero == null )
return true;
else
return false;
}
1
// ───────────────────────────────────────
// MÉTODO: Insertar un Nodo al Principio
// ───────────────────────────────────────
public void InsertarPrimero(Object dato){
// Paso 1: Crear un nodo temporal Nodo
temp = new Nodo( dato );
if( estaVacio() ){
Primero = temp; // Si está vacío
}
else{
//Paso 2: Enlazamos temp.sig → Primero
temp.sig = Primero;
//Paso 3: Enlazamos temp ← Primero.ant
Primero.ant = temp;
//Paso 4: Actualizamos el Primero Primero =
temp;
}
}
// ────────────────────────
// MÉTODO: Mostrar la lista
// ────────────────────────
public void MostrarLista(){
Nodo aux = Primero;
while( aux != null ){ System.out.println(
aux.info ); aux = aux.sig;
//avanzamos al sgte
}
// ────────────────────────
// MÉTODO: Eliminar Primero
// ────────────────────────
public Object EliminarPrimero(){
Object dato;
if( Primero == null){
return(null);
}
else if( Primero.ant == null && Primero.sig == null){ dato
= Primero.info; // Salvar la info
Primero = null;
return(dato);
}
else{
dato = Primero.info; // Salvar la info
Primero = Primero.sig; // Avanzar al sgte. nodo
Primero.ant = null; // El 1er nodo apuntar a null
return(dato);
}
}
2
// ──────────────────────────────────
// MÉTODO: Obtener el Primer elemento
// ──────────────────────────────────
public Object getPrimero(){
return(Primero.info);
}
// ─────────────────────────────────────────────────
// MÉTODO: Obtener la cantidad de elementos (tamaño)
// ─────────────────────────────────────────────────
public int getCantidad(){
Nodo aux = Primero; int
c=0;
while( aux != null ){
c++;
aux = aux.sig; // Avanzar
}
return(c);
}
// ──────────────────────────────────
// MÉTODO: Obtener el último elemento
// ──────────────────────────────────
public Object getUltimo(){
Nodo aux = Primero;
Object dato=null;
while( aux != null){
dato = aux.info; // Salvar info
aux = aux.sig; // Avanzar
}
return(dato);
}
// ───────────────────────────────────
// MÉTODO: Eliminar el último elemento
// ───────────────────────────────────
public Object EliminarUltimo(){
Nodo aux = Primero;
Object dato = null;
while( aux.sig != null ){ aux
= aux.sig; // Avanzar
}
dato = aux.info; // Salvar la info
aux = aux.ant; // Retroceder un nodo atrás aux.sig =
null; // apuntamos a null return(dato);
}
3
// ──────────────────────────────────────────────────
// MÉTODO: Eliminar los elementos impares de la lista
// ──────────────────────────────────────────────────
public void
Elimina
rImpare
s(){
Nodo
aux =
Primero
;
int elem;
while( aux != null ){
// Obtener el dato del nodo
elem =
Integer.parseInt(aux.info.to
String()); if (elem % 2 ==
1){
// el anterior nodo unimos con el
siguiente aux.ant.sig = aux.sig;
// el anterior del siguiente,
unimos con el anterior aux.sig.ant
= aux.ant;
}
aux = aux.sig; // Avanzar
}
} // Fin
4
4) ListaDoble1: Programa Principal
// Main: Programa Principal
package listadoble1; public
class ListaDoble1 {
public static void main(String[] args) {
ListaD lis = new ListaD();
lis.InsertarPrimero("2");
lis.InsertarPrimero("4");
lis.InsertarPrimero("5");
lis.InsertarPrimero("6");
lis.InsertarPrimero("8");
System.out.println("Lista de elementos:");
lis.MostrarLista();
System.out.println("1er nodo eliminado: " + lis.EliminarPrimero());
System.out.println("Lista de elementos:");
lis.MostrarLista();
System.out.println("Primero: " + lis.getPrimero()); if(
lis.estaVacio() )
System.out.println("Está vacía");
else
System.out.println("Último: " + lis.getUltimo());
System.out.println("Eliminar último: " + lis.EliminarUltimo());
System.out.println("Lista de elementos:");
lis.MostrarLista();
lis.EliminarImpares();
System.out.println("Lista de elementos actualizado:");
lis.MostrarLista();
5
5) ListaDoble1: Salida