Dev-san

Untitled

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