Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. package programa;
  2.  
  3. import listaSimplementeEnlazada.*;
  4.  
  5. public class IntercalarListas<E> {
  6.    
  7.    
  8.     protected PositionList<E> lista1; //estas listas están ordenadas
  9.     protected PositionList<E> lista2;
  10.  
  11.    
  12.     public IntercalarListas(PositionList<E> l1, PositionList<E> l2) {
  13.         lista1 = l1;
  14.         lista2 = l2;
  15.     }
  16.    
  17.    
  18.     public PositionList<E> intercalar() throws EmptyListException, BoundaryViolationException, InvalidPositionException {
  19.         NodeList<E> resultado = new NodeList<E>();
  20.         DefaultComparator<E> comp = new DefaultComparator<E>();
  21.         Position<E> p1 = lista1.first();
  22.         Position<E> p2 = lista2.first();
  23.         while ((p1 != lista1.last()) && (p2 != lista2.last())) {
  24.             if (comp.compare(p1.element(), p2.element()) == 0) {
  25.                 resultado.addLast(p1.element());
  26.                 p1 = lista1.next(p1);
  27.                 p2 = lista2.next(p2);
  28.             }
  29.             else {
  30.                 if (comp.compare(p1.element(), p2.element()) > 0) {
  31.                     resultado.addLast(p2.element());
  32.                     p2 = lista2.next(p2);
  33.                 }
  34.                 else {
  35.                     if (comp.compare(p1.element(), p2.element()) < 0) {
  36.                         resultado.addLast(p1.element());
  37.                         p1 = lista1.next(p1);
  38.                     }
  39.                 }
  40.             }
  41.         }
  42.         if (p1 == lista1.last()) {
  43.             resultado.addLast(p1.element());
  44.             p1 = null;
  45.         }
  46.         if (p2 == lista2.last()) {
  47.             resultado.addLast(p2.element());
  48.             p2 = null;
  49.         }
  50.         if (p1 != null) {
  51.             while (p1 != lista1.last()) {
  52.             resultado.addLast(p1.element());
  53.             p1 = lista1.next(p1);
  54.             }
  55.             resultado.addLast(p1.element());
  56.         }
  57.         if (p2 != null) {
  58.             while (p2 != lista2.last()) {
  59.             resultado.addLast(p2.element());
  60.             p2 = lista2.next(p2);
  61.             }
  62.             resultado.addLast(p2.element());
  63.         }
  64.        
  65.         return resultado;
  66.     }
  67.    
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement