miércoles, 30 de abril de 2014

Lista Simple

/**
 * @(#)ListaSimple.java
 *
 *
 * @author HERYCK MISAEL MUÑOZ MORA
 * @version 1.00 2014/4/29
 */
import java.util.Scanner;

class Nodo{ //va a estar trabajando mientras el programa se encuentre en ejecucion, es la clase que va a fabricar los NODOS
int elemento;
Nodo siguiente;
//Nodo anterior;

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



public class ListaSimple {
     
    /**
     * Creates a new instance of <code>ListaSimple</code>.
     */
    private static Nodo frente = null;
private static Nodo fin = null;
   
    public ListaSimple() {
    }
 
    /**
     * @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;
    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 ()
    {
    if ( frente == null)
    System.out.println("¡¡¡ La Lista esta VACIA !!!");
   
    else
    {
    System.out.println("elemento "+frente.elemento);
    frente = frente.siguiente;
        }
           
    }
 
     public static void limp()
    {        
   
    for (;frente != null;)
    {
    System.out.println("elemento "+frente.elemento);
    frente = frente.siguiente;
    }
    }  
 
    //tarea eliminar y limpiar
}

No hay comentarios:

Publicar un comentario