Kemudraj

SpecialSLLJoin

Nov 9th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.80 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.     // atributi
  9.     protected E element;// kreirame jazel so seuste nepoznat value E
  10.     protected SLLNode<E> succ;// link do sledniot jazel
  11.  
  12.     // konstruktor
  13.     public SLLNode(E element, SLLNode<E> succ) {
  14.         this.element = element;
  15.         this.succ = succ;
  16.     }
  17. }
  18.  
  19. class SLL<E> {
  20.     // atributi
  21.     private SLLNode<E> first;// prv jazel
  22.     // konstruktor - NE PRIMA ARGUMENTI
  23.  
  24.     public SLL() {
  25.         // kreiranje prazna lista
  26.         this.first = null;// go postavuvame prviot jazel da pokazuva na null
  27.     }
  28.  
  29.     // vrakja SLLNode
  30.     public SLLNode<E> getFirst() {
  31.         return first;
  32.     }
  33.  
  34.     public void insertFirst(E o) {
  35.         // napravi nov jazel koj ima value o i pokazuva na first
  36.         SLLNode<E> ins = new SLLNode<E>(o, first);
  37.         first = ins;// prviot jazel e noviot jazel
  38.     }
  39.  
  40.     public void insertLast(E o) {
  41.         if (first != null) {// ako ne e posleden
  42.             SLLNode<E> tmp = first;// kreiram temporary jazel
  43.             while (tmp.succ != null)// dodeka temporary ne e posleden
  44.                 tmp = tmp.succ;// izminuvaj ja listata, noviot temporary e
  45.                                 // sledniot
  46.             // ako stasas do posledniot
  47.             // napravi nov jazel koj ima value o i pokazuva na null
  48.             SLLNode<E> ins = new SLLNode<E>(o, null);
  49.             tmp.succ = ins;// sledniot jazel na posledniot neka bide noviot
  50.                             // jazel
  51.         } else {// ako prviot e i posleden i e ednakov na null
  52.             insertFirst(o);// vnesi kako prv i edinstven element
  53.         }
  54.     }
  55.  
  56.     // LOGIKA
  57.     public SLL<E> specialJoin(SLL<E> in2) {
  58.  
  59.         SLL<E> rezultat = new SLL<E>();
  60.         SLLNode<E> nodeLista1 = this.getFirst();
  61.         SLLNode<E> nodeLista2 = in2.getFirst();
  62.         boolean flag = true;
  63.         boolean flag1 = true;
  64.         while (nodeLista1 != null && nodeLista2 != null) {
  65.  
  66.             if (nodeLista1.succ == null && nodeLista2.succ == null) {
  67.                 rezultat.insertLast(nodeLista1.element);
  68.                 rezultat.insertLast(nodeLista2.element);
  69.                 return rezultat;
  70.             }
  71.             // ako sledbenikot na first ne e null
  72.             if (nodeLista1.succ != null) {
  73.                 rezultat.insertLast(nodeLista1.element);
  74.                 nodeLista1 = nodeLista1.succ;// odi na sleden
  75.                 rezultat.insertLast(nodeLista1.element);
  76.                 nodeLista1 = nodeLista1.succ;
  77.             } else if (flag) {// ako sledbenikot e null, vmetni go first i stavi
  78.                                 // lock
  79.                 rezultat.insertLast(nodeLista1.element);
  80.                 flag = false;
  81.             }
  82.             // ako sledbenikot na first ne e null
  83.             if (nodeLista2.succ != null) {
  84.                 rezultat.insertLast(nodeLista2.element);
  85.                 nodeLista2 = nodeLista2.succ;// odi na sleden
  86.                 rezultat.insertLast(nodeLista2.element);
  87.                 nodeLista2 = nodeLista2.succ;
  88.             } else if (flag1) {// ako e null, vmetni go first i stavi lock
  89.                 rezultat.insertLast(nodeLista2.element);
  90.                 flag1 = false;
  91.             }
  92.  
  93.         }
  94.         if (flag)
  95.             if (nodeLista1 != null) {// lista2 dosla do kraj
  96.                 while (nodeLista1 != null) {
  97.                     rezultat.insertLast(nodeLista1.element);
  98.                     nodeLista1 = nodeLista1.succ;
  99.                 }
  100.             }
  101.         if (flag1)
  102.             if (nodeLista2 != null) {// lista1 dosla do kraj
  103.                 while (nodeLista2 != null) {
  104.  
  105.                     rezultat.insertLast(nodeLista2.element);
  106.                     nodeLista2 = nodeLista2.succ;
  107.                 }
  108.             }
  109.         return rezultat;
  110.     }
  111.  
  112.     public Iterator<E> iterator() {
  113.         // vraka iterator koj gi posetuva site elementi na listata od levo na
  114.         // desno
  115.         return new LRIterator<E>();
  116.     }
  117.  
  118.     // //////////Inner class ////////////
  119.     private class LRIterator<E> implements Iterator<E> {
  120.         private SLLNode<E> place, prev, curr;
  121.  
  122.         private LRIterator() {
  123.             place = (SLLNode<E>) first;
  124.             curr = prev = null;
  125.         }
  126.  
  127.         public boolean hasNext() {
  128.             return (place != null);
  129.         }
  130.  
  131.         public E next() {
  132.             if (place == null)
  133.                 throw new NoSuchElementException();
  134.             E nextElem = place.element;
  135.             prev = curr;
  136.             curr = place;
  137.             place = place.succ;
  138.             return nextElem;
  139.         }
  140.  
  141.         public void remove() {
  142.             // Not implemented
  143.         }
  144.     }
  145. }
  146.  
  147. public class SpecialSLLJoin {
  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.         SLL<Integer> lista1 = new SLL<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.         SLL<Integer> lista2 = new SLL<Integer>();
  166.         for (int i = 0; i < N; i++) {
  167.             lista2.insertLast(Integer.parseInt(pomniza[i]));
  168.         }
  169.  
  170.         SLL<Integer> spoeni = lista1.specialJoin(lista2);
  171.  
  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