Don't like ads? PRO users don't see any ads ;-)
Guest

Manejo De listas en Java

By: Full_Programacion on Jun 22nd, 2012  |  syntax: None  |  size: 6.46 KB  |  hits: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class nodo {
  2.     int num = 0;
  3.         nodo sig;
  4.         void LLenar(nodo nodo_nuevo,int n)
  5.         {
  6.           nodo_nuevo.num = n;
  7.           nodo_nuevo.sig = null;        
  8.         }
  9. }
  10.  
  11. class Lista {
  12.     nodo L;
  13.  
  14.         void ingresar(int n){
  15.                 nodo nodo_nuevo = new nodo();
  16.                 if(L == null){                        
  17.                          nodo_nuevo.LLenar(nodo_nuevo,n);
  18.                          L = nodo_nuevo;
  19.                         }else{
  20.                         nodo_nuevo.LLenar(nodo_nuevo, n);
  21.                         nodo_nuevo.sig = L;
  22.                         L = nodo_nuevo;                        
  23.                         }
  24.         }
  25.        
  26.         void ingresar_final(nodo Aux, int n){
  27.                
  28.                 if(Aux == null){  
  29.                          nodo nodo_nuevo = new nodo();
  30.                          nodo_nuevo.LLenar(nodo_nuevo,n);
  31.                          L = nodo_nuevo;
  32.                         }else
  33.                            if(Aux.sig == null){
  34.                           nodo nodo_nuevo = new nodo();
  35.                           nodo_nuevo.LLenar(nodo_nuevo,n);
  36.                           Aux.sig= nodo_nuevo;                      
  37.                         }else{
  38.                                ingresar_final(Aux.sig,n);
  39.                            }
  40.         }
  41.  
  42.           void Eliminar_primero(){
  43.                 nodo Aux;
  44.                 Aux = L;
  45.                 if(Aux == null){  
  46.                         System.out.println("Lista Vacia");
  47.                         }else{
  48.                          L = L.sig;                      
  49.                         }
  50.         }
  51.          
  52.             void Eliminar_ultimo(nodo Aux){
  53.                
  54.                
  55.                 if(Aux == null){  
  56.                         System.out.println("Lista Vacia");
  57.                           if(Aux == L){
  58.                              L = null;                          
  59.                         }
  60.                 }else{
  61.                     if(Aux.sig.sig == null){
  62.                         Aux.sig = null;
  63.                        }else{
  64.                           Eliminar_ultimo(Aux.sig);
  65.                        }
  66.                  }
  67.         }
  68.           void Eliminar_Numero(nodo Aux,nodo Aux2,int n){
  69.              if(L == null){
  70.                  System.out.println("No se Encontro Elemento");
  71.              }else{
  72.                  if(Aux.sig == null){
  73.                      if(Aux.num == n){
  74.                          L = null;
  75.                      }else{
  76.                          System.out.println("No Encontrado");
  77.                      }
  78.                  }else{
  79.                      if(L.num == n){
  80.                          L = Aux.sig;
  81.                          Aux = null;
  82.                      }else{
  83.                          if(Aux.sig.num == n){
  84.                              Aux2 = Aux.sig;
  85.                              Aux.sig = Aux2.sig;
  86.                              Aux2 = null;
  87.                          }else{
  88.                              Eliminar_Numero(Aux.sig, Aux2, n);
  89.                          }
  90.                      }
  91.                  }
  92.              }                
  93.           }
  94.                    
  95.          
  96.         void mostrar(nodo L){
  97.  
  98.         if(L != null){
  99.                 System.out.println("Numero: "+L.num);
  100.                 mostrar(L.sig);
  101.         }else{
  102.                 System.out.println("Fin Lista");
  103.              }
  104.         }
  105. }
  106.  
  107.  
  108. import java.io.*;
  109.  
  110. public class Main {
  111.  
  112.     public static void main(String[] args) throws IOException {
  113.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  114.         Lista obj = new Lista();
  115.         Boolean Salir;
  116.         int opc = 0; int numero = 0;
  117.          
  118.         do{
  119.             Salir = false;
  120.             System.out.println("\t\tPROCESADOR DE LISTAS\n");
  121.             System.out.println("1. Ingresar Inicio");
  122.             System.out.println("2. Ingresar Final");
  123.             System.out.println("3. Eliminar Primero");
  124.             System.out.println("4. Eliminar Ultimo");
  125.             System.out.println("5. Eliminar Numero");
  126.             System.out.println("6. Mostrar");
  127.             System.out.println("7. Salir");
  128.             System.out.print("Opcion: ");
  129.             try{
  130.                 opc = Integer.parseInt(in.readLine());
  131.             }catch(NumberFormatException e){
  132.                 System.out.println("Debes Ingresar Solo Numeros");                
  133.             }
  134.             switch(opc){
  135.                 case 1:  
  136.                     System.out.println("\n\nBienvenido al Metodo Ingresar Inicio");
  137.                     System.out.print("Numero: ");
  138.                     try{
  139.                      numero = Integer.parseInt(in.readLine());
  140.                      }catch(NumberFormatException e){
  141.                       System.out.println("Debes Ingresar Solo Numeros");                
  142.                      }                    
  143.                     obj.ingresar(numero);
  144.                 break;
  145.                 case 2:
  146.                      System.out.println("\n\nBienvenido al Metodo Ingresar Final");
  147.                     System.out.print("Numero: ");
  148.                     try{
  149.                      numero = Integer.parseInt(in.readLine());
  150.                      }catch(NumberFormatException e){
  151.                       System.out.println("Debes Ingresar Solo Numeros");                
  152.                      }
  153.                    obj.ingresar_final(obj.L,numero);
  154.                 break;  
  155.                    
  156.                 case 3:
  157.                    obj.Eliminar_primero();
  158.                 break;  
  159.                 case 4:
  160.                    obj.Eliminar_ultimo(obj.L);
  161.                 break;  
  162.                 case 5:
  163.                     System.out.println("\n\nBienvenido al Metodo Eliminar Numero");
  164.                     System.out.print("Numero: ");
  165.                     try{
  166.                      numero = Integer.parseInt(in.readLine());
  167.                      }catch(NumberFormatException e){
  168.                       System.out.println("Debes Ingresar Solo Numeros");                
  169.                      }
  170.                    obj.Eliminar_Numero(obj.L,obj.L,numero);
  171.                 break;  
  172.                 case 6:
  173.                     System.out.println("\n******************");
  174.                     obj.mostrar(obj.L);  
  175.                     System.out.println("******************");
  176.                 break;  
  177.                 case 7:
  178.                     System.out.println("Saliendo del Programa");
  179.                     Salir = true;
  180.                 break;    
  181.                 default:
  182.                     System.out.println("\nOpcion Incorrecta");
  183.                 break;    
  184.             };
  185.         }while(Salir != true);
  186.     }
  187. }