Crazy

Спој листи наизменично

Oct 21st, 2017
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.NoSuchElementException;
  5. import java.util.Iterator;
  6.  
  7. class SLLNode<E> {
  8.     protected E element;
  9.     protected SLLNode<E> successor;
  10.    
  11.     public SLLNode(E element, SLLNode<E> successor) {
  12.         this.element = element;
  13.         this.successor = successor;
  14.     }
  15. }
  16.  
  17. class SLL<E> {
  18.     private SLLNode<E> first;
  19.    
  20.     public SLL() {
  21.         first = null;
  22.     }
  23.    
  24.     public SLLNode<E> getFirst() {
  25.         return first;
  26.     }
  27.    
  28.     public void insertFirst(E o) {
  29.         SLLNode<E> insert = new SLLNode<E>(o, first);
  30.         first = insert;
  31.     }
  32.    
  33.     public void insertLast(E o) {
  34.         if(first != null) {
  35.             SLLNode<E> tmp = first;
  36.             while(tmp.successor != null)
  37.                 tmp = tmp.successor;
  38.             SLLNode<E> insert = new SLLNode<E>(o, null);
  39.             tmp.successor = insert;            
  40.         } else insertFirst(o);
  41.     }
  42.    
  43.     public Iterator<E> iterator() {
  44.         return new LRIterator();
  45.     }
  46.    
  47.     private class LRIterator<E> implements Iterator<E> {
  48.         private SLLNode<E> place, current;
  49.        
  50.         private LRIterator() {
  51.             place = (SLLNode<E>)first;
  52.             current = null;
  53.         }
  54.        
  55.         public boolean hasNext() {
  56.             return place != null;
  57.         }
  58.        
  59.         public E next() {
  60.             if(place == null)
  61.                 throw new NoSuchElementException();
  62.             E nextElement = place.element;
  63.             current = place;
  64.             place = place.successor;
  65.             return nextElement;
  66.         }
  67.     }
  68. }
  69.  
  70. public class SpecialSLLJoin {
  71.    
  72.     public static SLL<Integer> specialJoin(SLL<Integer> listOne, SLL<Integer> listTwo) {
  73.         SLLNode<Integer> listOneNode = listOne.getFirst(), listTwoNode = listTwo.getFirst();
  74.         SLL<Integer> specialOne = new SLL<Integer>();
  75.         while(listOneNode != null&&listTwoNode != null) {
  76.             specialOne.insertLast(listOneNode.element);
  77.             if(listOneNode.successor != null) {
  78.                 listOneNode = listOneNode.successor;
  79.                 specialOne.insertLast(listOneNode.element);
  80.             }
  81.             listOneNode = listOneNode.successor;
  82.            
  83.             specialOne.insertLast(listTwoNode.element);
  84.             if(listTwoNode.successor != null) {
  85.                 listTwoNode = listTwoNode.successor;
  86.                 specialOne.insertLast(listTwoNode.element);
  87.             }
  88.             listTwoNode = listTwoNode.successor;
  89.         }
  90.        
  91.         if(listOneNode != null) {
  92.             while(listOneNode != null) {
  93.                 specialOne.insertLast(listOneNode.element);
  94.                 listOneNode = listOneNode.successor;
  95.             }
  96.         }
  97.        
  98.         if(listTwoNode != null) {
  99.             while(listTwoNode != null) {
  100.                 specialOne.insertLast(listTwoNode.element);
  101.                 listTwoNode = listTwoNode.successor;
  102.             }
  103.         }
  104.        
  105.         return specialOne;
  106.     }
  107.    
  108.     public static void main(String[] args) throws IOException{
  109.        
  110.         SLL<Integer> lista1 = new SLL<Integer>();
  111.         SLL<Integer> lista2 = new SLL<Integer>();
  112.    
  113.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  114.                 System.in));
  115.         String s = stdin.readLine();
  116.         int N = Integer.parseInt(s);
  117.         s = stdin.readLine();
  118.         String[] pomniza = s.split(" ");
  119.         for (int i = 0; i < N; i++) {
  120.             lista1.insertLast(Integer.parseInt(pomniza[i]));
  121.         }
  122.  
  123.         s = stdin.readLine();
  124.         N = Integer.parseInt(s);
  125.         s = stdin.readLine();
  126.         pomniza = s.split(" ");
  127.         for (int i = 0; i < N; i++) {
  128.             lista2.insertLast(Integer.parseInt(pomniza[i]));
  129.         }
  130.        
  131.         SLL<Integer> spoeni = specialJoin(lista1,lista2);
  132.        
  133.         Iterator<Integer> it = spoeni.iterator();
  134.         while(it.hasNext()) {
  135.             System.out.print(it.next());
  136.             if(it.hasNext())
  137.                 System.out.print(" ");
  138.         }      
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment