elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


  Mostrar Temas
Páginas: [1]
1  Programación / Java / ejemplo de lista con nodo en: 25 Julio 2012, 22:52 pm
el codigo es el siguiente:


public class nodo {
   protected int  info;
   protected nodo nexo;
   
   nodo() {}
   nodo(int info) {
      this.info = info;
      this.nexo = null;
   }}
public class Lista extends nodo {
   nodo Inicio, Ultimo;
   
   Lista()
   {
      Inicio= null;
      Ultimo= null;
   }
   
   public void agregar(nodo nuevo)
   {
      if(Ultimo==null)
          Inicio = nuevo;
      else       
          Ultimo.nexo = nuevo;
      Ultimo = nuevo;
   }
   
   public void mostrar()
   {
      System.out.print("Lista");
      nodo p=Inicio;
      while(p!=null) {
         System.out.print(" - " + p.info);
         p = p.nexo;
      }
      System.out.println();
   }
   
   // METODOS QUE FALTAN POR CODIFICAR
   
   public boolean existeNodoCon(int info)
   {
       nodo p=Inicio;
       while(p!=null)
       {
           if(p.info==info)
           {
               return true;
           }
           p=p.nexo;
       }
       return false;
   }
   
   public void eliminaPrimero()
   {
       if(Ultimo!=null)
       {
           if(Inicio.nexo==null)
           {
               Inicio=null;
               Ultimo=null;
           }
           else
           {
               Inicio=Inicio.nexo;
           }
       }
   }
   
   public void eliminaUltimo()
   {
       nodo p=Inicio;
       while(p!=Ultimo)
       {
           if(p.nexo==Ultimo)
           {
               p.nexo=null;
               Ultimo=p;
           }
           else
           {
               p=p.nexo;
           }
       }
   }
   
 
   public void muestraAlreves()
   {
       Alveres(Inicio);
       System.out.println();
   }
   
 
   private void Alveres(nodo p)
   {
       if(p.nexo==null)
       {
           System.out.print(p.info+"-");       }
       else
       {
           Alveres(p.nexo);
           System.out.print(p.info+"-");
       }   }     
 public boolean vacia()
   {
       if(Inicio==null)
       {
           return true;
       }
       else return false;
   }
      public void EliCon(int dato)
   {
       nodo ant=Inicio;
       nodo p=Inicio;
       while(p!=null)
       {
           if(p.info==dato)
           {
               if(p==Inicio)
               {
                   eliminaPrimero();
               }
               else
               {                   ant.nexo=p.nexo;
                   p=p.nexo;
               }           }
           else
           {               ant=p;
               p=p.nexo;
           }       }   }
      public void Inter(int dato)
   {
       nodo p=Inicio;
       nodo ant=Inicio;
       nodo q,r;
       while(p!=null)
       {
           if(p.info==dato)
           {
               if(p==Inicio)
               {
                   q=p.nexo;
                   r=q.nexo;
                   Inicio=q;
                   Inicio.nexo=p;
                   p.nexo=r;
                   ant=p;
                   p=p.nexo;
               }
               else
               {
                   if(p.nexo!=null)
                   {
                       q=p.nexo;
                       r=q.nexo;
                       p.nexo=r;
                       ant.nexo=q;
                       q.nexo=p;
                       ant=p;
                       p=p.nexo;
                                 }   } }
           else{
           ant=p;
           p=p.nexo;}
       }   }}


public class principal{
   public static void main(String args[]) {                                     
     
     Lista miLista = new Lista ();                                                         
     nodo nuevo;
       int  info=0, inf;
     
     for(int i=0; i<10; i++) {                         
         if(Math.random()>0.7) info = 12;               
         else info = 10 + (int)(Math.random()*90);       
         nuevo = new nodo(info);
         miLista.agregar(nuevo);
         miLista.mostrar();
        }
        miLista.EliCon(12);                               
        miLista.mostrar();                                 
       
        System.out.println();
        while(!miLista.vacia()) miLista.eliminaPrimero(); 

     

        for(int i=0; i<10; i++) {                         
            if(Math.random()>0.7) info = 12;               
            else info = 10 + (int)(Math.random()*90);       
            nuevo = new nodo(info);
            miLista.agregar(nuevo);
            miLista.mostrar();
        }
        miLista.Inter(12);                                 
        miLista.mostrar();                                 
       
     
        System.out.println();
       while(!miLista.vacia()) miLista.eliminaPrimero(); 


    for(int i=0; i<10; i++) {                         
        if(Math.random()>0.7) info = 12;               
        else info = 10 + (int)(Math.random()*90);       
        if(i==8) info =12;                             
        nuevo = new nodo(info);
        miLista.agregar(nuevo);
        miLista.mostrar();
    }
    miLista.Inter(12);                                 
    miLista.mostrar();                                 

     
      System.out.println();                                                     
      inf = info;                                                               
                                                                               
      System.out.print("nodo de contenido " + inf);     
      if(miLista.existeNodoCon(info)) System.out.println(" esta");         
      else                            System.out.println(" No esta");         
                                                                                 
      info = 10 + (int)(Math.random()*90);                                       
      System.out.print(" nodo de contenido " + info);   
      if(miLista.existeNodoCon(info)) System.out.println(" esta");         
      else                            System.out.println(" No esta");         
                                                                                 
      System.out.print("elimina el primer nodo ");                     
      miLista.eliminaPrimero();                                                 
      miLista.mostrar();                                                         
                                                                                 
      System.out.print("elimina ultimo nodo  ");                     
      miLista.eliminaUltimo();                                                   
      miLista.mostrar();                                                         
                                                                                 
      System.out.print("mostrando al reves  ");                       
      miLista.muestraAlreves();                                                 
                                                                                 
   }                                                                             
}                                                                               

Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines