Kemudraj

SLLJoinLists

Nov 9th, 2017
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.45 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. //KLASA ZA JAZEL
  8. class SLLNode<E> {
  9.     // atributi
  10.     protected E element;// kreirame jazel so seuste nepoznat value E
  11.     protected SLLNode<E> succ;// link do sledniot jazel
  12.  
  13.     // konstruktor
  14.     public SLLNode(E element, SLLNode<E> succ) {
  15.         this.element = element;
  16.         this.succ = succ;
  17.     }
  18. }
  19.  
  20. // KLASA ZA LISTA
  21. class SLL<E extends Comparable<E>> {
  22.     // atributi
  23.     private SLLNode<E> first;// prv jazel
  24.     // konstruktor - NE PRIMA ARGUMENTI
  25.  
  26.     public SLL() {
  27.         // kreiranje prazna lista
  28.         this.first = null;// go postavuvame prviot jazel da pokazuva na null
  29.     }
  30.  
  31.     // vrakja SLLNode
  32.     public SLLNode<E> getFirst() {
  33.         return first;
  34.     }
  35.  
  36.     public void insertFirst(E o) {
  37.         // napravi nov jazel koj ima value o i pokazuva na first
  38.         SLLNode<E> ins = new SLLNode<E>(o, first);
  39.         first = ins;// prviot jazel e noviot jazel
  40.     }
  41.  
  42.     public void insertLast(E o) {
  43.         if (first != null) {// ako ne e posleden
  44.             SLLNode<E> tmp = first;// kreiram temporary jazel
  45.             while (tmp.succ != null)// dodeka temporary ne e posleden
  46.                 tmp = tmp.succ;// izminuvaj ja listata, noviot temporary e
  47.                                 // sledniot
  48.             // ako stasas do posledniot
  49.             // napravi nov jazel koj ima value o i pokazuva na null
  50.             SLLNode<E> ins = new SLLNode<E>(o, null);
  51.             tmp.succ = ins;// sledniot jazel na posledniot neka bide noviot
  52.                             // jazel
  53.         } else {// ako prviot e i posleden i e ednakov na null
  54.             insertFirst(o);// vnesi kako prv i edinstven element
  55.         }
  56.     }
  57.  
  58.     // go povikuvame so lista2
  59.     // metodata vrakja niza od jazli
  60.     public SLL<E> joinLists(SLL<E> in) {
  61.         // TODO Auto-generated method stub
  62.         // deklarirame niza sto treba da ja vratime od metodata
  63.         SLL<E> rezultat = new SLL<E>();
  64.  
  65.         // kreirame 2 novi jazli, prviot od prvata lista,
  66.         // vtoriot od vtorata i prvicno pokazuvaat na prviot jazel
  67.         SLLNode<E> nodeLista1 = this.getFirst();
  68.         SLLNode<E> nodeLista2 = in.getFirst();
  69.  
  70.         while (nodeLista1 != null && nodeLista2 != null) {
  71.             // ako e pomal od vtoriot element, ke vrati negativen broj
  72.             // hence the < 0
  73.             if (nodeLista1.element.compareTo(nodeLista2.element) < 0) {
  74.                 rezultat.insertLast(nodeLista1.element);// smesti vo novata nizaS
  75.                                                         // // pomaliot
  76.                 nodeLista1 = nodeLista1.succ;// odi na sledniot jazel
  77.             } else {// ako ne e pomal vrati go drugiot
  78.                 rezultat.insertLast(nodeLista2.element);
  79.                 nodeLista2 = nodeLista2.succ;// odi na sledniot jazel
  80.             }
  81.         }
  82.         // sto ako edniot e null, a drugiot ne e ?
  83.         // ako vtoriot dosol do kraj a prviot seuste ne
  84.         if (nodeLista1 != null) {
  85.             while (nodeLista1 != null) {// izmini gi site ostanati i smesti gi
  86.                                         // vo lista
  87.                 rezultat.insertLast(nodeLista1.element);
  88.                 nodeLista1 = nodeLista1.succ;
  89.             }
  90.         }
  91.         // soodvetno ako prviot dosol do kraj, smesti gi preostanatite jazli od
  92.         // vtorata lista
  93.         if (nodeLista2 != null) {
  94.             while (nodeLista2 != null) {
  95.                 rezultat.insertLast(nodeLista2.element);
  96.                 nodeLista2 = nodeLista2.succ;
  97.             }
  98.         }
  99.  
  100.         // kreiraj nova prazna lista
  101.         SLL<E> rezultatBezDuplikati = new SLL<E>();
  102.         // kreiraj 2 novi nodes, prviot so prviot element od rezultat
  103.         // vtoriot so negoviot sledbenik
  104.         SLLNode<E> novNode = rezultat.getFirst();
  105.         SLLNode<E> tmpNode = novNode.succ;
  106.         while (tmpNode != null) {
  107.             // ako prviot e ist so vtoriot
  108.             if (novNode.element.compareTo(tmpNode.element) == 0) {
  109.                 novNode = novNode.succ;// primi ja vrednosta od vtoriot
  110.                 tmpNode = tmpNode.succ;// vtoriot na negoviot sledbenik
  111.             } else {// ako se razlicni
  112.                 rezultatBezDuplikati.insertLast(novNode.element);
  113.                 novNode = novNode.succ;// odi na sleden
  114.                 tmpNode = tmpNode.succ;// odi na sleden
  115.             }
  116.         }
  117.         rezultatBezDuplikati.insertLast(novNode.element);
  118.         return rezultatBezDuplikati;
  119.     }
  120.  
  121.     public Iterator<E> iterator() {
  122.         // vraka iterator koj gi posetuva site elementi na listata od levo na
  123.         // desno
  124.         return new LRIterator<E>();
  125.     }
  126.  
  127.     // //////////Inner class ////////////
  128.     private class LRIterator<E> implements Iterator<E> {
  129.         private SLLNode<E> place, prev, curr;
  130.  
  131.         private LRIterator() {
  132.             place = (SLLNode<E>) first;
  133.             curr = prev = null;
  134.         }
  135.  
  136.         public boolean hasNext() {
  137.             return (place != null);
  138.         }
  139.  
  140.         public E next() {
  141.             if (place == null)
  142.                 throw new NoSuchElementException();
  143.             E nextElem = place.element;
  144.             prev = curr;
  145.             curr = place;
  146.             place = place.succ;
  147.             return nextElem;
  148.         }
  149.  
  150.         public void remove() {
  151.             // Not implemented
  152.         }
  153.     }
  154.  
  155. }
  156.  
  157. public class SLLJoinLists {
  158.     public static void main(String[] args) throws IOException {
  159.  
  160.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  161.         String s = stdin.readLine();
  162.         int N = Integer.parseInt(s);
  163.         s = stdin.readLine();
  164.         String[] pomniza = s.split(" ");
  165.         SLL<Integer> lista1 = new SLL<Integer>();
  166.         for (int i = 0; i < N; i++) {
  167.             lista1.insertLast(Integer.parseInt(pomniza[i]));
  168.         }
  169.  
  170.         s = stdin.readLine();
  171.         N = Integer.parseInt(s);
  172.         s = stdin.readLine();
  173.         pomniza = s.split(" ");
  174.         SLL<Integer> lista2 = new SLL<Integer>();
  175.         for (int i = 0; i < N; i++) {
  176.             lista2.insertLast(Integer.parseInt(pomniza[i]));
  177.         }
  178.  
  179.         SLL<Integer> spoeni = lista1.joinLists(lista2);
  180.         Iterator<Integer> it = spoeni.iterator();
  181.         while (it.hasNext()) {
  182.             System.out.print(it.next());
  183.             if (it.hasNext())
  184.                 System.out.print(" ");
  185.         }
  186.         System.out.println();
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment