Advertisement
196040

APS labs 2 Spoj listi

Nov 25th, 2020 (edited)
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.38 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.NoSuchElementException;
  3. import java.util.Iterator;
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. class SLLNode<E> {
  8.     protected E element;
  9.     protected SLLNode<E> succ;
  10.  
  11.     public SLLNode(E elem, SLLNode<E> succ) {
  12.         this.element = elem;
  13.         this.succ = succ;
  14.     }
  15.  
  16.     @Override
  17.     public String toString() {
  18.         return element.toString();
  19.     }
  20. }
  21. class SLL<E> {
  22.     private SLLNode<E> first;
  23.     public SLL() {// Construct an empty SLL
  24.         this.first = null;
  25.     }
  26.     public void deleteList() {
  27.         first = null;
  28.     }
  29.     public int length() {
  30.         int ret;
  31.         if (first != null) {
  32.             SLLNode<E> tmp = first;
  33.             ret = 1;
  34.             while (tmp.succ != null) {
  35.                 tmp = tmp.succ;
  36.                 ret++;
  37.             }
  38.             return ret;
  39.         } else
  40.             return 0;
  41.     }
  42.     @Override
  43.     public String toString() {
  44.         String ret = new String();
  45.         if (first != null) {
  46.             SLLNode<E> tmp = first;
  47.             ret += tmp + "->";
  48.             while (tmp.succ != null) {
  49.                 tmp = tmp.succ;
  50.                 ret += tmp + "->";
  51.             }
  52.         } else
  53.             ret = "Prazna lista!!!";
  54.         return ret;
  55.     }
  56.     public void insertFirst(E o) {
  57.         SLLNode<E> ins = new SLLNode<E>(o, first);
  58.         first = ins;
  59.     }
  60.     public void insertAfter(E o, SLLNode<E> node) {
  61.         if (node != null) {
  62.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  63.             node.succ = ins;
  64.         } else {
  65.             System.out.println("Dadenot jazol e null");
  66.         }
  67.     }
  68.     public void insertBefore(E o, SLLNode<E> before) {
  69.         if (first != null) {
  70.             SLLNode<E> tmp = first;
  71.             if (first == before) {
  72.                 this.insertFirst(o);
  73.                 return;
  74.             }//ako first!=before
  75.             while (tmp.succ != before)
  76.                 tmp = tmp.succ;
  77.             if (tmp.succ == before) {
  78.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  79.                 tmp.succ = ins;
  80.             } else {
  81.                 System.out.println("Elementot ne postoi vo listata");
  82.             }
  83.         } else {
  84.             System.out.println("Listata e prazna");
  85.         }
  86.     }
  87.     public void insertLast(E o) {
  88.         if (first != null) {
  89.             SLLNode<E> tmp = first;
  90.             while (tmp.succ != null)
  91.                 tmp = tmp.succ;
  92.             SLLNode<E> ins = new SLLNode<E>(o, null);
  93.             tmp.succ = ins;
  94.         } else {
  95.             insertFirst(o);
  96.         }
  97.     }
  98.     public E deleteFirst() {
  99.         if (first != null) {
  100.             SLLNode<E> tmp = first;
  101.             first = first.succ;
  102.             return tmp.element;
  103.         } else {
  104.             System.out.println("Listata e prazna");
  105.             return null;
  106.         }
  107.     }
  108.     public E delete(SLLNode<E> node) {
  109.         if (first != null) {
  110.             SLLNode<E> tmp = first;
  111.             if (first == node) {
  112.                 return this.deleteFirst();
  113.             }
  114.             while (tmp.succ != node&&tmp.succ.succ != null)
  115.                 tmp = tmp.succ;
  116.             if (tmp.succ == node) {
  117.                 tmp.succ = tmp.succ.succ;
  118.                 return node.element;
  119.             } else {
  120.                 System.out.println("Elementot ne postoi vo listata");
  121.                 return null;
  122.             }
  123.         } else {
  124.             System.out.println("Listata e prazna");
  125.             return null;
  126.         }
  127.     }
  128.     public SLLNode<E> getFirst() {
  129.         return first;
  130.     }
  131.     public SLLNode<E> find(E o) {
  132.         if (first != null) {
  133.             SLLNode<E> tmp = first;
  134.             while (tmp.element != o && tmp.succ != null)
  135.                 tmp = tmp.succ;
  136.             if (tmp.element == o) {
  137.                 return tmp;
  138.             } else {
  139.                 System.out.println("Elementot ne postoi vo listata");
  140.             }
  141.         } else {
  142.             System.out.println("Listata e prazna");
  143.         }
  144.         return first;
  145.     }
  146.     public Iterator<E> iterator() {
  147.         // Return an iterator that visits all elements of this list, in left-to-right order.
  148.         return new LRIterator<E>();
  149.     } // //////////Inner class ////////////
  150.     private class LRIterator<E> implements Iterator<E> {
  151.         private SLLNode<E> place, curr;
  152.         private LRIterator() {
  153.             place = (SLLNode<E>) first;
  154.             curr = null;
  155.         }
  156.         public boolean hasNext() {
  157.             return (place != null);
  158.         }
  159.         public E next() {
  160.             if (place == null)
  161.                 throw new NoSuchElementException();
  162.             E nextElem = place.element;
  163.             curr = place;
  164.             place = place.succ;
  165.             return nextElem;
  166.         }
  167.     }
  168.     public void mirror() {
  169.         if (first != null) {
  170.             //m=nextsucc, p=tmp,q=next
  171.             SLLNode<E> tmp = first;
  172.             SLLNode<E> newsucc = null;
  173.             SLLNode<E> next;
  174.             while (tmp != null) {
  175.                 next = tmp.succ;
  176.                 tmp.succ = newsucc;
  177.                 newsucc = tmp;
  178.                 tmp = next;
  179.             }
  180.             first = newsucc;
  181.         }
  182.     }
  183.     public void merge(SLL<E> in) {
  184.         if (first != null) {
  185.             SLLNode<E> tmp = first;
  186.             while (tmp.succ != null)
  187.                 tmp = tmp.succ;
  188.             tmp.succ = in.getFirst();
  189.         } else {
  190.             first = in.getFirst();
  191.         }
  192.     }
  193.     public void reverse(SLL list) {
  194.         SLLNode<E> counter = list.getFirst();
  195.         SLL<E> novo = new SLL<>();
  196.         while (counter != null) {
  197.             novo.insertLast((E) counter);
  198.             counter = counter.succ;
  199.         }
  200.         counter = novo.getFirst();
  201.         while (counter != null) {
  202.             System.out.print(counter + " -> ");
  203.             counter = counter.succ;
  204.         }
  205.     }
  206.     public SLL sortiraj() {
  207.         SLLNode<E> temp = getFirst();//        node temp = head;
  208.         while (temp != null) {  // Traverse the List
  209.             SLLNode<E> min = temp; //        node min = temp;
  210.             SLLNode<E> r = temp.succ;//   node r = temp.next;
  211.             // Traverse the unsorted sublist
  212.             while (r != null) {
  213.                 if ((int) min.element > (int) r.element) //  if (min.data > r.data)
  214.                     min = r;
  215.                 r = r.succ; //r = r.next;
  216.             }
  217.             // Swap Data
  218.             E x = temp.element; //int x = temp.data;
  219.             temp.element = min.element; //temp.data = min.data;
  220.             min.element = x; //min.data = x;
  221.             temp = temp.succ; //temp = temp.next;
  222.         }
  223.         return this;
  224.     }
  225.     public void removeDuplicates() {
  226.         SLLNode<E> amanvise = getFirst();
  227.         while(amanvise.succ!=null) {
  228.             if(amanvise.element == amanvise.succ.element)
  229.                 delete(amanvise);
  230.             amanvise = amanvise.succ;
  231.         }
  232.     }
  233. }
  234. public class Pile {
  235. //Klasata mi se vika Pile oti vaa e stara laboratoriska
  236. //Ama duri sega a resavam i ne znam kako treba da se vika
  237.     public static void main(String[] args) throws IOException {
  238.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  239.         String s = stdin.readLine();
  240.         int N = Integer.parseInt(s);
  241.         s = stdin.readLine();
  242.         String[] pomniza = s.split(" ");
  243.         SLL lista1 = new SLL();
  244.         for (int i = 0; i < N; i++) {
  245.             lista1.insertLast(Integer.parseInt(pomniza[i]));
  246.         }
  247.         s = stdin.readLine();
  248.         N = Integer.parseInt(s);
  249.         s = stdin.readLine();
  250.         pomniza = s.split(" ");
  251.         SLL lista2 = new SLL();
  252.         for (int i = 0; i < N; i++) {
  253.             lista2.insertLast(Integer.parseInt(pomniza[i]));
  254.         }
  255.         lista1.merge(lista2);
  256.         SLL spoeni = new SLL();
  257.         spoeni = lista1.sortiraj();
  258.         spoeni.removeDuplicates();
  259.         Iterator<Integer> it = spoeni.iterator();
  260.         while (it.hasNext()) {
  261.             System.out.print(it.next());
  262.             if(it.hasNext())
  263.                 System.out.print(" ");
  264.         }
  265.         System.out.println();
  266.     }
  267. }
  268. //tekstot e lost ama treba da se spoat 2 listi i da se sortirat u rastecki redosled
  269. //duplikatite u nizata da se ostranat
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement