DamSi

Untitled

Aug 23rd, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1.  
  2. public class JoinSortedLists<E extends Comparable<E>> {
  3.  
  4.     public SLL<E> join(SLL<E> list1, SLL<E> list2){
  5.         SLL<E> rezultat = new SLL<E>();
  6.         SLLNode<E> jazol1 = list1.getFirst(), jazol2 = list2.getFirst();
  7.         //SLLNode<E> jazol2 = list2.getFirst();
  8.        
  9.         while(jazol1 != null && jazol2 != null){           
  10.             if(jazol1.element.compareTo(jazol2.element)<0){ //jazol1<jazol2
  11.                 rezultat.insertLast(jazol1.element);
  12.                 jazol1 = jazol1.succ;
  13.             }
  14.             else{
  15.                 rezultat.insertLast(jazol2.element);
  16.                 jazol2 = jazol2.succ;
  17.             }
  18.            
  19.         }
  20.        
  21.         if(jazol1 != null){
  22.             while(jazol1 != null){
  23.                 rezultat.insertLast(jazol1.element);
  24.                 jazol1 = jazol1.succ;
  25.             }
  26.         }
  27.        
  28.         if(jazol2 != null){
  29.             while(jazol2 != null){
  30.                 rezultat.insertLast(jazol2.element);
  31.                 jazol2 = jazol2.succ;
  32.             }
  33.         }
  34.        
  35.         return rezultat;
  36.     }
  37.    
  38.     public static void main(String[] args){
  39.         SLL<String> lista1 = new SLL<String>();
  40.         lista1.insertLast("Ana");lista1.insertLast("Bojana");lista1.insertLast("Dejan");
  41.         SLL<String> lista2 = new SLL<String>();
  42.         lista2.insertLast("Andrijana");lista2.insertLast("Biljana");lista2.insertLast("Darko");
  43.        
  44.         JoinSortedLists<String> js = new JoinSortedLists<String>();
  45.         System.out.println(js.join(lista1, lista2));
  46.        
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment