Dev-san

Untitled

Oct 19th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.68 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.util.Iterator;
  11. import java.util.NoSuchElementException;
  12.  
  13.  
  14. public class SLLJoinLists {
  15.    
  16.     public class Node<E>
  17.     {
  18.         private E element;
  19.         private Node<E> succ;
  20.        
  21.         public Node (E element,Node<E> succ)
  22.         {
  23.             this.element=element;
  24.             this.succ=succ;
  25.         }
  26.        
  27.        
  28.            
  29.     }
  30.    
  31.     public class Iter<E> implements Iterator<E>
  32.             {
  33.         Node<E> atm,sledno;
  34.        
  35.         Iter(Node<E> sledno)
  36.                 {
  37.                         this.sledno = sledno;
  38.                         atm = sledno;
  39.                 }
  40.        
  41.         public boolean hasNext()
  42.         {
  43.             if(sledno==null)
  44.                 return false;
  45.             else
  46.                 return true;
  47.         }
  48.        
  49.        
  50.         public E next()
  51.         {
  52.             if (atm == null)
  53.                 System.out.println("Atm e null");
  54.             E nov=atm.element;
  55.             if(hasNext()==true)
  56.             {
  57.                 nov=sledno.element;
  58.                 atm=sledno;
  59.                 sledno=sledno.succ;
  60.                  
  61.             }
  62.            
  63.            return nov;
  64.            
  65.         }
  66.     }
  67.    
  68.     public class Lista<E>
  69.     {
  70.         Node<E> prv,posleden;
  71.        
  72.         Lista()
  73.         {
  74.             prv=null;
  75.             posleden=null;
  76.         }
  77.        
  78.         Iter<E> iterator()
  79.         {
  80.             return new Iter<E>(prv);
  81.         }
  82.        
  83.         public void insertLast(E element)
  84.         {
  85.             if(prv==null)
  86.             {
  87.                 Node<E> novo=new Node<E>(element, prv);
  88.                 prv=novo;
  89.                 posleden=novo;
  90.             }
  91.             else
  92.             {
  93.                 Node<E>novo=new Node<E>(element,null);
  94.                 posleden.succ = novo;
  95.                 posleden=novo;
  96.             }
  97.         }
  98.     }
  99.    
  100.     public <E extends Comparable <E>> Lista<E> merge (Lista<E> prva, Lista<E> vtora)
  101.     {
  102.         Lista <E> spoena=new Lista<E>();
  103.         Node<E> jazol1=prva.prv,jazol2=vtora.prv;
  104.         while(jazol1!=null&&jazol2!=null)
  105.         {
  106.             if((Integer)jazol1.element<(Integer)jazol2.element && jazol1.element != spoena.posleden.element)
  107.             {
  108.                 spoena.insertLast(jazol1.element);
  109.                 jazol1=jazol1.succ;
  110.             }
  111.             else if ((Integer)jazol1.element>(Integer)jazol2.element && jazol2.element != spoena.posleden.element)
  112.             {
  113.                
  114.                 spoena.insertLast(jazol2.element);
  115.                 jazol2=jazol2.succ;
  116.             }
  117.             else
  118.             {
  119.                 if (spoena.posleden != null)
  120.                 {
  121.                     if ((jazol1 != null && jazol1.element != spoena.posleden.element)
  122.                             || (jazol2 != null && jazol2.element != spoena.posleden.element))
  123.                         spoena.insertLast(jazol1.element);
  124.                 }
  125.                 else
  126.                     spoena.insertLast(jazol1.element);
  127.                 jazol1 = jazol1.succ;
  128.                 jazol2 = jazol2.succ;
  129.             }
  130.         }
  131.        
  132.         while(jazol1!=null)
  133.         {
  134.             spoena.insertLast(jazol1.element);
  135.             jazol1=jazol1.succ;
  136.         }
  137.        
  138.         while(jazol2!=null)
  139.         {
  140.             spoena.insertLast(jazol2.element);
  141.             jazol2=jazol2.succ;
  142.         }
  143.        
  144.         return spoena;
  145.     }
  146.    
  147.    
  148.    
  149.     public static void main(String[] args) throws IOException {
  150.  
  151.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  152.         String s = stdin.readLine();
  153.         int N = Integer.parseInt(s);
  154.         s = stdin.readLine();
  155.         String[] pomniza = s.split(" ");
  156.                 SLLJoinLists join=new SLLJoinLists();
  157.                 Lista<Integer> lista1 = join.new Lista<Integer>();
  158.                 Lista<Integer> lista2 = join.new Lista<Integer>();
  159.         for (int i = 0; i < N; i++) {
  160.             lista1.insertLast(Integer.parseInt(pomniza[i]));
  161.         }
  162.  
  163.         s = stdin.readLine();
  164.         N = Integer.parseInt(s);
  165.         s = stdin.readLine();
  166.         pomniza = s.split(" ");
  167.         for (int i = 0; i < N; i++) {
  168.             lista2.insertLast(Integer.parseInt(pomniza[i]));
  169.         }
  170.  
  171.         Lista<Integer> spoeni = join.merge(lista1,lista2);
  172.         Iterator<Integer> it = spoeni.iterator();
  173.         while (it.hasNext()) {
  174.             System.out.print(it.next());
  175.             if(it.hasNext())
  176.                 System.out.print(" ");
  177.         }
  178.         System.out.println();
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment