miércoles, 14 de noviembre de 2012

Estructura de Datos 3S21 TESOEM

Codigo para eliminar nodos de la lista:

/**
* @(#)MenuListas.java
*
*
* @author Victor Hugo De la O
* @version 2.00 2012/11/14
*/
import java.util.Scanner;

public class MenuListas {
    Scanner teclado = new Scanner(System.in);

    class nodo{
        String nombre;
        nodo siguiente;
    }
    public nodo agregar(nodo datos){
        String nombre;
        nodo temp = new nodo();
        if (datos == null){
        System.out.println ("Ingrese el nombre: ");
        nombre = teclado.next();
        temp.nombre=nombre;
        temp.siguiente=null;
        datos=temp;
        }
        else{
        System.out.println ("Ingrese el nombre: ");
        nombre = teclado.next();
            temp.nombre=nombre;
            temp.siguiente=datos;
            datos=temp;
        }
        return datos;
    }
    public nodo Elimina(nodo datos){
        int posicion=0,lugar=0;
        boolean encontrado=true;
        String nombre;
        nodo temp = new nodo();
        System.out.println ("Que nombre deseas eliminar");
        nombre=teclado.next();
        temp = datos;
        while (temp != null){
            if (temp.nombre.equals(nombre))
                lugar=posicion;
            posicion++;
            temp = temp.siguiente;
        }       
            System.out.println ("esta en la posicion " +lugar);
            if (lugar == contar(datos)){               
                temp = datos;
                posicion=0;
                while (encontrado){
                    if (posicion == (lugar-1)){
                        encontrado=false;
                    }
                    else
                        temp=temp.siguiente;
                    posicion++;                   
                }
                temp.siguiente=null;
            }
            else if (lugar ==0 ){
                    temp = datos.siguiente;
                    datos=temp;               
                System.out.println ("esta en la posicion maxima no se puede eliminar directo nodo.sig->nodo.sig.sig");
            }
            else{
                temp = datos;
                posicion=0;
                while (encontrado){
                    if (posicion == (lugar-1)){
                        encontrado=false;
                    }
                    else
                        temp=temp.siguiente;
                    posicion++;                   
                }
                temp.siguiente=temp.siguiente.siguiente;
            }
        return datos;
    }
    public int contar(nodo datos){
        int posicion=0;
        while (datos.siguiente != null){
            posicion++;
            datos = datos.siguiente;
        }
        System.out.println ("numero de registro "+posicion);
      return posicion;
    }
    public void mostrar(nodo datos){
        while (datos != null){
         System.out.println (datos.nombre);
         datos=datos.siguiente;
        }
    }
    public void menu(){
      System.out.println ("Menu de opciones");
      System.out.println ("[A] Agregar");
      System.out.println ("[M] Mostrar");
      System.out.println ("[E] Eliminar");
      System.out.println ("[S] Salir");
      System.out.println ("\tQue opcion deseas-> ");
    }

    public MenuListas(){
        char opc;
        String opcion;
        boolean bandera = true;
        nodo objdatos = new nodo();
        objdatos = null;
        while (bandera){
        try{
        menu();
        //opc = (char)System.in.read();
        opcion = teclado.next();
        opcion=opcion.toUpperCase();
        opc=opcion.charAt(0);
        switch(opc){
            case 'A':
            case 'a': objdatos = agregar(objdatos);break;
            case 'M':
            case 'm':mostrar(objdatos); break;
            case 'E':
            case 'e':objdatos = Elimina(objdatos);break;
            case 'S':
            case 's':bandera=false; break;
        }
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
        }
    }
    public static void main (String[] args) {
        MenuListas objlistas = new MenuListas();
}      
}