velimir

ListiSmthn

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