Advertisement
Turisas

Listas

Jun 28th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.33 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package ayudantia;
  6.  
  7.  
  8.  
  9. /**
  10.  *
  11.  * @author EDWARD
  12.  */
  13. public class Lista {
  14.     Nodo L;
  15.     public Lista()
  16.     {
  17.         L=null;
  18.     }
  19.     public void InsertarP(int a)
  20.     {
  21.         Nodo p= new Nodo(a);
  22.         if (L==null)
  23.         {
  24.                 L=p;
  25.         }
  26.         else
  27.         {
  28.         p.setEnlase(L);
  29.         L=p;
  30.         }
  31.     }
  32.     public void InsertarU(int d)
  33.     {
  34.         Nodo t= new Nodo (d);
  35.         if (L==null)
  36.         {
  37.             L=t;
  38.         }
  39.         else
  40.         {
  41.         Nodo aux=L;
  42.             while (aux!=null)
  43.             {
  44.                 if (aux.getEnlase()==null)
  45.                 {
  46.                     aux.setEnlase(t);
  47.                     return;
  48.                 }
  49.                 aux=aux.getEnlase();
  50.             }
  51.         }
  52.     }
  53.     public String mostrar()
  54.     {   String s="";
  55.         Nodo aux=L;
  56.         while(aux!=null)
  57.         {
  58.         s=s+" | "+aux.getDato();
  59.         aux=aux.getEnlase();
  60.         }
  61.         return s;
  62.     }
  63.            
  64.    private boolean vacio()
  65.    {    
  66.        if(L==null)
  67.            return true;
  68.        else
  69.            return false;
  70.       }      
  71. public int Suma () throws Exception
  72. {
  73.   int x=0;
  74.   if(L==null){
  75.     throw new Exception("Error : La lista esta vacia") ;
  76.   }else{
  77.       Nodo P=L;
  78.       while (P!=null ){
  79.         x=x+P.getDato();
  80.         P=P.getEnlase();
  81.        
  82.       }
  83.   return x;
  84.   }
  85.    }
  86. public int mayor() throws Exception
  87. {      
  88.   int may;
  89.   Nodo aux=L;
  90.   if (L!=null)
  91.   {
  92.      may=aux.getDato();
  93.      while(aux!=null)
  94.       {    
  95.         if (aux.getDato()>may){
  96.             may=aux.getDato();
  97.         }
  98.         aux=aux.getEnlase();
  99.        }
  100.    return may;
  101.    }            
  102. else{
  103.       throw new Exception("Error");
  104.      }  
  105.    }  
  106. public void eliminar(int dato){
  107.      Nodo P=L;
  108.      
  109.      if (vacio())
  110.      {
  111.      }
  112.          else
  113.      {
  114.      
  115.      if (L.getEnlase()==null)
  116.      {
  117.             if (L.getDato()==dato)
  118.                 L=null;
  119.      }
  120.      else
  121.      {
  122.          if (P.getDato()==dato)
  123.          {
  124.              L=L.getEnlase();
  125.          }
  126.          else
  127.          {
  128.          if(P.getEnlase()!=null) {
  129.         while(P!=null)
  130.          {  
  131.                     if (dato==P.getEnlase().getDato()){
  132.             P.setEnlase(P.getEnlase().getEnlase());
  133.                     return;
  134.                     }
  135.                     else {
  136.                 P =P.getEnlase();
  137.             }
  138.         }    }  
  139. }
  140.      }
  141.      }
  142. }
  143. public boolean esta(int x) {
  144. Nodo aux=L;
  145. while(aux!=null){
  146. if(x==aux.getDato()){
  147.     return true;
  148. }else{
  149.    
  150. aux=aux.getEnlase();
  151. }
  152. }
  153. return false;
  154. }
  155. public void eliminarUlt()
  156. {       Nodo aux=L;
  157.             while (aux!=null){
  158.                 if (aux.getEnlase().getEnlase()==null)
  159.                     aux.setEnlase(null);
  160.                 aux=aux.getEnlase();
  161.             }
  162.  
  163. }
  164. public void eliminarPri()
  165. {    
  166.     if(vacio())
  167.    return;
  168.     L=L.getEnlase();
  169.  
  170. }
  171. public void ordenarAsc () throws Exception{
  172.   if (L==null){
  173.       throw new Exception("error");
  174.   }  
  175.   else{
  176.      //Nodo aux=L;
  177.      Lista p=new Lista();
  178.      while(L!=null){
  179.         int x=mayor();
  180.         eliminar(x);
  181.         p.InsertarU(x);
  182.         //L=L.getEnlase();
  183.      }
  184.     // Lista aux=p;
  185.      /*while(p.L!=null){
  186.           InsertarP(p.L.getDato());
  187.           p.L=p.L.getEnlase();
  188.      
  189.      }*/
  190.      L=p.L;
  191.      //return;
  192.   }
  193.  
  194. }
  195.  
  196.   public void Invertir() throws Exception{
  197.   if (L==null){
  198.       throw new Exception("error");
  199.   }  
  200.   else{
  201.      //Nodo aux=L;
  202.      Lista p=new Lista();
  203.      while(L!=null){
  204.         int x=L.getDato();
  205.         eliminar(x);
  206.         p.InsertarU(x);
  207.         //L=L.getEnlase();
  208.      }
  209.     // Lista aux=p;
  210.      while(p.L!=null){
  211.           InsertarP(p.L.getDato());
  212.           p.eliminarPri();
  213.      
  214.      }
  215.      //L=p.L;
  216.      //return;
  217.   }
  218. }
  219.  
  220.  
  221.  
  222.  
  223. //---------------------------------RECURSIVO----------------------------------------
  224.  
  225. public int  Sumarrr()
  226. {
  227. return SumarRec(L);
  228. }
  229. private int SumarRec(Nodo p)
  230. {
  231. int suma=0;
  232. if (p==null)
  233. {
  234. return suma;
  235. }
  236. else
  237. {
  238.   suma=suma+SumarRec(p.getEnlase());
  239.     return suma+p.getDato();
  240. }
  241. }
  242.  
  243. public void Elimnarrr(int dato)
  244. {   EliminarR(L, dato);
  245. }
  246.  private void EliminarR(Nodo p,int dato)
  247.  {
  248.        Nodo P=L;
  249.      
  250.      if (vacio())
  251.      {
  252.          return;
  253.      }
  254.          else
  255.      {
  256.      
  257.      if (L.getEnlase()==null)
  258.      {
  259.             if (L.getDato()==dato)
  260.                 L=null;
  261.             return;
  262.                
  263.      }
  264.      else
  265.      {
  266.          if (P.getDato()==dato)
  267.          {
  268.              L=L.getEnlase();
  269.              return;
  270.          }
  271.          else
  272.          {
  273.          if(P.getEnlase()!=null) {
  274.                      EliminarR(p.getEnlase(), dato);
  275.                     if (dato==P.getEnlase().getDato()){
  276.                    P.setEnlase(P.getEnlase().getEnlase());
  277.                     return;
  278.                     }
  279.        
  280.         }    }  
  281. }
  282.      }
  283.      }
  284.  public void invertirdosendos(){
  285.      
  286.      Lista p=new Lista();
  287.      while (L!=null){
  288.      
  289.          int x=0;
  290.          while(x!=2){
  291.            
  292.              Invertir();
  293.      
  294.             x++;
  295.         }
  296.      }
  297.  
  298.  
  299.  }
  300.    
  301.  
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement