Advertisement
Guest User

SpojListiNaizmenicno

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