Dev-san

Untitled

Oct 19th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 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)
  107.             {
  108.                 spoena.insertLast(jazol1.element);
  109.                 jazol1=jazol1.succ;
  110.             }
  111.             else if ((Integer)jazol1.element>(Integer)jazol2.element)
  112.             {
  113.                
  114.                 spoena.insertLast(jazol2.element);
  115.                 jazol2=jazol2.succ;
  116.             }
  117.             else
  118.             {
  119.                 spoena.insertLast(jazol1.element);
  120.                 jazol1 = jazol1.succ;
  121.                 jazol2 = jazol2.succ;
  122.             }
  123.         }
  124.        
  125.         while(jazol1!=null)
  126.         {
  127.             spoena.insertLast(jazol1.element);
  128.             jazol1=jazol1.succ;
  129.         }
  130.        
  131.         while(jazol2!=null)
  132.         {
  133.             spoena.insertLast(jazol2.element);
  134.             jazol2=jazol2.succ;
  135.         }
  136.        
  137.         return spoena;
  138.     }
  139.    
  140.    
  141.    
  142.     public static void main(String[] args) throws IOException {
  143.  
  144.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  145.         String s = stdin.readLine();
  146.         int N = Integer.parseInt(s);
  147.         s = stdin.readLine();
  148.         String[] pomniza = s.split(" ");
  149.                 SLLJoinLists join=new SLLJoinLists();
  150.                 Lista<Integer> lista1 = join.new Lista<Integer>();
  151.                 Lista<Integer> lista2 = join.new Lista<Integer>();
  152.         for (int i = 0; i < N; i++) {
  153.             lista1.insertLast(Integer.parseInt(pomniza[i]));
  154.         }
  155.  
  156.         s = stdin.readLine();
  157.         N = Integer.parseInt(s);
  158.         s = stdin.readLine();
  159.         pomniza = s.split(" ");
  160.         for (int i = 0; i < N; i++) {
  161.             lista2.insertLast(Integer.parseInt(pomniza[i]));
  162.         }
  163.  
  164.         Lista<Integer> spoeni = join.merge(lista1,lista2);
  165.         Iterator<Integer> it = spoeni.iterator();
  166.         while (it.hasNext()) {
  167.             System.out.print(it.next());
  168.             if(it.hasNext())
  169.                 System.out.print(" ");
  170.         }
  171.         System.out.println();
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment