Dev-san

Untitled

Oct 19th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 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.         @Override
  50.         public void remove()
  51.         {
  52.            
  53.         }
  54.        
  55.         public E next()
  56.         {
  57.             if (atm == null)
  58.                 System.out.println("Atm e null");
  59.             E nov=atm.element;
  60.             if(hasNext()==true)
  61.             {
  62.                 nov=sledno.element;
  63.                 atm=sledno;
  64.                 sledno=sledno.succ;
  65.                  
  66.             }
  67.            
  68.            return nov;
  69.            
  70.         }
  71.     }
  72.    
  73.     public class Lista<E>
  74.     {
  75.         Node<E> prv,posleden;
  76.        
  77.         Lista()
  78.         {
  79.             prv=null;
  80.             posleden=null;
  81.         }
  82.        
  83.         Iter<E> iterator()
  84.         {
  85.             return new Iter<E>(prv);
  86.         }
  87.        
  88.         public void insertLast(E element)
  89.         {
  90.             if(prv==null)
  91.             {
  92.                 Node<E> novo=new Node<E>(element, prv);
  93.                 prv=novo;
  94.                 posleden=novo;
  95.             }
  96.             else
  97.             {
  98.                 Node<E>novo=new Node<E>(element,null);
  99.                 posleden.succ = novo;
  100.                 posleden=novo;
  101.             }
  102.         }
  103.     }
  104.    
  105.     public <E extends Comparable <E>> Lista<E> merge (Lista<E> prva, Lista<E> vtora)
  106.     {
  107.         Lista <E> spoena=new Lista<E>();
  108.         Node<E> jazol1=prva.prv,jazol2=vtora.prv;
  109.         while(jazol1!=null&&jazol2!=null)
  110.         {
  111.             if((Integer)jazol1.element<(Integer)jazol2.element)
  112.             {
  113.                 spoena.insertLast(jazol1.element);
  114.                 jazol1=jazol1.succ;
  115.             }
  116.             else if ((Integer)jazol1.element>(Integer)jazol2.element)
  117.             {
  118.                
  119.                 spoena.insertLast(jazol2.element);
  120.                 jazol2=jazol2.succ;
  121.             }
  122.             else
  123.             {
  124.                 spoena.insertLast(jazol1.element);
  125.                 jazol1 = jazol1.succ;
  126.                 jazol2 = jazol2.succ;
  127.             }
  128.         }
  129.        
  130.         while(jazol1!=null)
  131.         {
  132.             spoena.insertLast(jazol1.element);
  133.             jazol1=jazol1.succ;
  134.         }
  135.        
  136.         while(jazol2!=null)
  137.         {
  138.             spoena.insertLast(jazol2.element);
  139.             jazol2=jazol2.succ;
  140.         }
  141.        
  142.         return spoena;
  143.     }
  144.    
  145.    
  146.    
  147.     public static void main(String[] args) throws IOException {
  148.  
  149.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  150.         String s = stdin.readLine();
  151.         int N = Integer.parseInt(s);
  152.         s = stdin.readLine();
  153.         String[] pomniza = s.split(" ");
  154.                 SLLJoinLists join=new SLLJoinLists();
  155.                 Lista<Integer> lista1 = join.new Lista<Integer>();
  156.                 Lista<Integer> lista2 = join.new Lista<Integer>();
  157.         for (int i = 0; i < N; i++) {
  158.             lista1.insertLast(Integer.parseInt(pomniza[i]));
  159.         }
  160.  
  161.         s = stdin.readLine();
  162.         N = Integer.parseInt(s);
  163.         s = stdin.readLine();
  164.         pomniza = s.split(" ");
  165.         for (int i = 0; i < N; i++) {
  166.             lista2.insertLast(Integer.parseInt(pomniza[i]));
  167.         }
  168.  
  169.         Lista<Integer> spoeni = join.merge(lista1,lista2);
  170.         Iterator<Integer> it = spoeni.iterator();
  171.         while (it.hasNext()) {
  172.             System.out.print(it.next());
  173.             if(it.hasNext())
  174.                 System.out.print(" ");
  175.         }
  176.         System.out.println();
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment