Advertisement
Turisas

ListaPares

Jul 10th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.60 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package lista1;
  6.  
  7. /**
  8.  *
  9.  * @author usuario
  10.  */
  11. public class Lista1 {nodo1 L;//referencia a la lista
  12.                      int dim; //numeros de elemento
  13.                      
  14.  //constructor        
  15.                      
  16. public Lista1(){
  17. L=null;
  18. dim=0;
  19. }
  20. public boolean vacia(){
  21.  return(L==null);
  22. }
  23. public boolean unico(){
  24. return (L.getEnlace()==null);
  25. }
  26. // METODO DE INSERTAR
  27. public void insertar(int elem){
  28. if(vacia())
  29. {
  30.  nodo1 P=new nodo1();  
  31.  P.setDato(elem);          
  32.  L=P;
  33.  dim++;
  34. }
  35. else{          
  36.  nodo1 q=L;nodo1 ant=null;              
  37.  while((q!=null)&&(q.getDato()<=elem)){
  38.  ant=q;
  39.  q=q.getEnlace();
  40. }
  41.  if(ant==null){// no se movio
  42.  nodo1 P=new nodo1(elem);          
  43.  L=P;                      
  44.  P.setEnlace(q);
  45.  dim++;
  46.  }else{    
  47.  if(ant.getDato()!=elem){    
  48.   nodo1 P=new nodo1(elem);
  49.   ant.setEnlace(P);
  50.   P.setEnlace(q);                
  51.   dim++;
  52.  }
  53.  }
  54. }
  55. }
  56.  public void InsertarP(int ele) {
  57.         nodo1 P = new nodo1(ele);
  58.         P.setEnlace(L);
  59.         L = P;
  60.         dim++;
  61.     }
  62.  
  63.     public void InsertarU(int ele) {
  64.         if (vacia()) {
  65.             InsertarP(ele);
  66.         } else {
  67.             nodo1 aux = L;
  68.             while ((aux.getEnlace() != null) && (aux.getDato() != ele)) {
  69.                 aux = aux.getEnlace();
  70.             }
  71.             if (aux.getDato() != ele) {
  72.                 nodo1 P = new nodo1(ele);
  73.                 aux.setEnlace(P);
  74.                 dim++;
  75.             }
  76.         }
  77.     }
  78. //insertar con elemetos repetidos
  79. public void insertar1(int d){
  80.     nodo1 p=new nodo1();
  81.     p.setDato(d);
  82.     p.setEnlace(L);
  83.     L=p;
  84.     dim++;
  85. }
  86. //INSETAR ORDENADO
  87.   public void InsertarOrd(int ele){
  88.         if (vacia()){
  89.             InsertarP(ele);
  90.         }else{//hay mas elementos
  91.             nodo1 Aux=L;
  92.             nodo1 Ant=null;
  93.             while ((Aux!=null)&&(Aux.getDato()<=ele)){
  94.                 Ant=Aux;
  95.                 Aux=Aux.getEnlace();
  96.             }
  97.                  if (Ant==null)
  98.                {
  99.                    InsertarP(ele);
  100.                }    
  101.                 else{//inserta en el cuerpo
  102.                    if (Ant.getDato()!=ele){  
  103.                       nodo1 P=new nodo1();
  104.                       P.setDato(ele);
  105.                       Ant.setEnlace(P);
  106.                       P.setEnlace(Aux);
  107.                    }  
  108.                    dim++;
  109.                 }    
  110.         }
  111.        
  112.     }
  113.  
  114. public void eliminar(int elem){
  115. if(!vacia()){
  116. nodo1 q=L;
  117. nodo1 ant=null;
  118. while((q!=null)&&(q.getDato()!=elem)){
  119. ant=q;
  120. q=q.getEnlace();
  121. }
  122. if(ant==null){
  123. L=L.getEnlace();
  124. dim--;
  125. }else{
  126. if(q!=null){
  127. ant.setEnlace(q.getEnlace());
  128. dim--;
  129. }
  130. }  
  131. }
  132. }
  133.  
  134.  
  135. public void pares(){
  136.    
  137.     if(!vacia()){
  138.        
  139.         int aux=L.getDato();
  140.        
  141.         L=L.getEnlace();
  142.        
  143.        
  144.         pares();
  145.        
  146.        
  147.         if(aux % 2==0){
  148.            
  149.            
  150.             if(L==null)
  151.                 L=new nodo1(aux);
  152.             else{
  153.                 nodo1 par=new nodo1(aux);
  154.                 par.setEnlace(L);
  155.                 L=par;
  156.             }
  157.         }      
  158.     }
  159. }
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167. @Override
  168.     public String toString() {
  169.      String s = "Lista --> < ";
  170.             nodo1 P = L;
  171.         while (P != null) {            
  172.             s = s + P.getDato() + ", ";
  173.             P = P.getEnlace();
  174.         }
  175.         return s + ">";
  176.     }
  177.  
  178.  
  179.     /**
  180.      * @param args the command line arguments
  181.      */
  182.     public static void main(String[] args) {
  183.         // TODO code application logic here
  184.         Lista1 L=new Lista1();
  185.         L.insertar(1);
  186.         L.insertar(2);
  187.         L.insertar(3);
  188.         L.insertar(4);
  189.         L.insertar(5);
  190.         L.insertar(6);
  191.         System.out.println(L.toString());
  192.        
  193.         L.pares();
  194.         System.out.println(L);
  195.     }
  196. }
  197.  
  198. <----- CODIGO DEL NODO ---->
  199.  
  200. /*
  201.  * To change this template, choose Tools | Templates
  202.  * and open the template in the editor.
  203.  */
  204. package lista1;
  205.  
  206. /**
  207.  *
  208.  * @author usuario
  209.  */
  210. public class nodo1 { int dato;
  211.                     nodo1 enlace;
  212.                    
  213.  public nodo1(){
  214.  enlace=null;
  215.  }
  216.  
  217.  public nodo1(int dato){
  218.  this.dato=dato;
  219.  enlace=null;
  220.  }
  221.  
  222.     public void setDato(int dato) {
  223.         this.dato = dato;
  224.     }
  225.  
  226.     public void setEnlace(nodo1 enlace) {
  227.         this.enlace = enlace;
  228.     }
  229.     public int getDato() {
  230.         return dato;
  231.     }
  232.  
  233.     public nodo1 getEnlace() {
  234.         return enlace;
  235.     }
  236.  
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement