Advertisement
Guest User

SpojListi

a guest
Oct 20th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Iterator;
  5.  
  6. class SLLNode<T> {
  7.    
  8.     protected T value;
  9.     protected SLLNode<T> next;
  10.    
  11.     public SLLNode(T value) {
  12.         this(value, null);
  13.     }
  14.    
  15.     public SLLNode(T value, SLLNode<T> next) {
  16.         this.value = value;
  17.         this.next = next;
  18.     }
  19.    
  20. }
  21.  
  22. class SLL<T> implements Iterable<T> {
  23.  
  24.     private SLLNode<T> first;
  25.    
  26.     public SLL() {
  27.         first = null;
  28.     }
  29.    
  30.     public void insertFirst(T element) {
  31.         SLLNode<T> newFirst = new SLLNode<T>(element, first);
  32.         this.first = newFirst;
  33.     }
  34.    
  35.     public void insertLast(T element) {
  36.         SLLNode<T> newLast = new SLLNode<T>(element);
  37.         SLLNode<T> last = getLast();
  38.         if(last != null) {
  39.             last.next = newLast;
  40.         } else {
  41.             insertFirst(element);
  42.         }
  43.     }
  44.    
  45.     private SLLNode<T> getLast() {
  46.         if(first != null) {
  47.             SLLNode<T> t = first;
  48.             while(t.next != null) {
  49.                 t = t.next;
  50.             }
  51.             return t;
  52.         }
  53.        
  54.         return null;
  55.     }
  56.    
  57.     public SLL<Integer> joinLists(SLL<Integer> list) {
  58.         SLL<Integer> newList = new SLL<Integer>();
  59.        
  60.         @SuppressWarnings("unchecked")
  61.         SLLNode<Integer> l1curr = (SLLNode<Integer>) first;
  62.         SLLNode<Integer> l2curr = list.first;
  63.         Integer lastInserted = null;
  64.         while(l1curr != null&&l2curr != null) {
  65.             int newVal = l1curr.value;
  66.             if(l1curr.value < l2curr.value) {
  67.                 l1curr = l1curr.next;
  68.             } else if(l1curr.value > l2curr.value) {
  69.                 newVal = l2curr.value;
  70.                 l2curr = l2curr.next;
  71.             } else {
  72.                 l1curr = l1curr.next;
  73.                 l2curr = l2curr.next;
  74.             }
  75.            
  76.             if(lastInserted != null) {
  77.                 if(newVal == lastInserted) continue;
  78.             }
  79.             lastInserted = newVal;
  80.             newList.insertLast(newVal);
  81.         }
  82.         while(l1curr != null) {
  83.             int newVal = l1curr.value;
  84.             l1curr = l1curr.next;
  85.            
  86.             if(lastInserted != null) {
  87.                 if(newVal == lastInserted) continue;
  88.             }
  89.             lastInserted = newVal;
  90.             newList.insertLast(newVal);
  91.         }
  92.         while(l2curr != null) {
  93.             int newVal = l2curr.value;
  94.             l2curr = l2curr.next;
  95.             if(lastInserted != null) {
  96.                 if(newVal == lastInserted) continue;
  97.             }
  98.             lastInserted = newVal;
  99.             newList.insertLast(newVal);
  100.         }
  101.        
  102.        
  103.         return newList;
  104.     }
  105.    
  106.     public void print() {
  107.         SLLNode<T> t = first;
  108.         while(t != null) {
  109.             System.out.println(t.value);
  110.             t = t.next;
  111.         }
  112.     }
  113.    
  114.     @Override
  115.     public Iterator<T> iterator() {
  116.         return new Iterator<T>() {
  117.            
  118.             private SLLNode<T> currentNode = first;
  119.  
  120.             @Override
  121.             public boolean hasNext() {
  122.                 return currentNode != null;
  123.             }
  124.  
  125.             @Override
  126.             public T next() {
  127.                 T value = currentNode.value;
  128.                 currentNode = currentNode.next;
  129.                 return value;
  130.             }
  131.            
  132.         };
  133.     }
  134.    
  135. }
  136.  
  137. public class SLLJoinLists {
  138.     public static void main(String[] args) throws IOException {
  139.         SLL<Integer> lista1 = new SLL<Integer>();
  140.         SLL<Integer> lista2 = new SLL<Integer>();
  141.        
  142.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  143.         String s = stdin.readLine();
  144.         int N = Integer.parseInt(s);
  145.         s = stdin.readLine();
  146.         String[] pomniza = s.split(" ");
  147.         for (int i = 0; i < N; i++) {
  148.             lista1.insertLast(Integer.parseInt(pomniza[i]));
  149.         }
  150.  
  151.         s = stdin.readLine();
  152.         N = Integer.parseInt(s);
  153.         s = stdin.readLine();
  154.         pomniza = s.split(" ");
  155.         for (int i = 0; i < N; i++) {
  156.             lista2.insertLast(Integer.parseInt(pomniza[i]));
  157.         }
  158.  
  159.         SLL<Integer> spoeni = lista1.joinLists(lista2);
  160.         Iterator<Integer> it = spoeni.iterator();
  161.         while (it.hasNext()) {
  162.             System.out.print(it.next());
  163.             if(it.hasNext())
  164.                 System.out.print(" ");
  165.         }
  166.         System.out.println();
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement