viernes, 2 de mayo de 2014

Lista Doble

/**
 * @(#)ListaDoble.java
 *
 *
 * @author HERYCK MISAEL MUÑOZ MORA
 * @version 1.00 2014/4/30
 */

import java.util.Scanner;

class Nodo
{
int elemento;
Nodo siguiente;
Nodo anterior;
public Nodo (int x)
{
elemento = x;
siguiente = null;
anterior = null;
}
}


public class ListaDoble {

private static Nodo frente = null;
private static Nodo fin = null;
       
    /**
     * Creates a new instance of <code>ListaDoble</code>.
     */
    public ListaDoble() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    Scanner ent = new Scanner (System.in);
        int opcion;
       
        do{
        System.out.println("\n");
        System.out.println("1.- INSERTAR ");
        System.out.println("2.- PRESENTAR ");
        System.out.println("3.-  ELIMINAR ");
        System.out.println("4.- LIMPIAR ");
        System.out.println("5.- TERMINAR");
       
       
        opcion = ent.nextInt();
       
        switch (opcion)
        {
        case 1: Insertar();
        break;
       
        case 2: presentar ();
        break;
       
        case 3: Elim();
        break;
       
        case 4: limp();
        break;
       
        case 5:
        break;
       
        default:
        System.out.println("Opcion equivocada");
       
       
        }         // cierra el switch
       
        }
        while(opcion != 5);         // cierra el do
    }
   
     public static void Insertar()
    {
    int valor;
    Nodo aux;    
    Scanner ent = new Scanner(System.in); // AQUI SE CREA EL OBJETO DE TIPO SCANNER, PARA SER INTRODUCIDO CON EL TECLADO
    System.out.print("Ingrese Elemento: ");
    valor = ent.nextInt();
   
    aux = new Nodo (valor); // creamos un nodo con ambas partes, donde contendra "valor", y en siguiente sera "null"
        if  (frente == null)
        {
        frente = aux;
        }
       
    else
    {
    fin.siguiente = aux;
    aux.anterior = fin;  
    }
    fin = aux;
    }
   
     public static void presentar()
    {
    Nodo aux;
    if ( frente == null)
    System.out.println("¡¡¡ La Lista esta VACIA !!!");
       
    for (aux = frente; aux != null;)
    {
    System.out.println("elemento "+aux.elemento);
    aux = aux.siguiente;
    }
    }
   
     public static void Elim ()
    { // elimina de fin a hacia "frente"
    Nodo aux;
    if ( frente == null)
    System.out.println("¡¡¡ La Lista esta VACIA !!!");
   
    else
    {
    if (frente == fin)
    {
    frente=fin=null;
    }
     
    else
    {
    aux=fin.anterior; // para borrar de frente a fin cambiar la palabra "fin" por la palabra "frente"
    fin.anterior=null;
    aux.siguiente=null;
    fin=aux;
    System.out.println("¡¡¡ se BORRO el ultimo elemento de la lista !!!");
    }
       
        }
       
           
    }
   
     public static void limp()
    {        
    for (; frente != null;)
    {
    System.out.println("elemento "+frente.elemento);
    frente = frente.siguiente;
    }
    }  
}