Advertisement
196040

APS labs 2 Spoj specijalni listi

Nov 25th, 2020
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.58 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.     public SLLNode(E elem, SLLNode<E> succ) {
  11.         this.element = elem;
  12.         this.succ = succ;
  13.     }
  14.     @Override
  15.     public String toString() {
  16.         return element.toString();
  17.     }
  18. }
  19.     class SLL<E> {
  20.         private SLLNode<E> first;
  21.         public SLL() {// Construct an empty SLL
  22.             this.first = null;
  23.         }
  24.         public void deleteList() {
  25.             first = null;
  26.         }
  27.         public int length() {
  28.             int ret;
  29.             if (first != null) {
  30.                 SLLNode<E> tmp = first;
  31.                 ret = 1;
  32.                 while (tmp.succ != null) {
  33.                     tmp = tmp.succ;
  34.                     ret++;
  35.                 }
  36.                 return ret;
  37.             } else
  38.                 return 0;
  39.         }
  40.         @Override
  41.         public String toString() {
  42.             String ret = new String();
  43.             if (first != null) {
  44.                 SLLNode<E> tmp = first;
  45.                 ret += tmp + "->";
  46.                 while (tmp.succ != null) {
  47.                     tmp = tmp.succ;
  48.                     ret += tmp + "->";
  49.                 }
  50.             } else
  51.                 ret = "Prazna lista!!!";
  52.             return ret;
  53.         }
  54.         public void insertFirst(E o) {
  55.             SLLNode<E> ins = new SLLNode<E>(o, first);
  56.             first = ins;
  57.         }
  58.         public void insertAfter(E o, SLLNode<E> node) {
  59.             if (node != null) {
  60.                 SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  61.                 node.succ = ins;
  62.             } else {
  63.                 System.out.println("Dadenot jazol e null");
  64.             }
  65.         }
  66.         public void insertBefore(E o, SLLNode<E> before) {
  67.             if (first != null) {
  68.                 SLLNode<E> tmp = first;
  69.                 if (first == before) {
  70.                     this.insertFirst(o);
  71.                     return;
  72.                 }//ako first!=before
  73.                 while (tmp.succ != before)
  74.                     tmp = tmp.succ;
  75.                 if (tmp.succ == before) {
  76.                     SLLNode<E> ins = new SLLNode<E>(o, before);
  77.                     tmp.succ = ins;
  78.                 } else {
  79.                     System.out.println("Elementot ne postoi vo listata");
  80.                 }
  81.             } else {
  82.                 System.out.println("Listata e prazna");
  83.             }
  84.         }
  85.         public void insertLast(E o) {
  86.             if (first != null) {
  87.                 SLLNode<E> tmp = first;
  88.                 while (tmp.succ != null)
  89.                     tmp = tmp.succ;
  90.                 SLLNode<E> ins = new SLLNode<E>(o, null);
  91.                 tmp.succ = ins;
  92.             } else {
  93.                 insertFirst(o);
  94.             }
  95.         }
  96.         public E deleteFirst() {
  97.             if (first != null) {
  98.                 SLLNode<E> tmp = first;
  99.                 first = first.succ;
  100.                 return tmp.element;
  101.             } else {
  102.                 System.out.println("Listata e prazna");
  103.                 return null;
  104.             }
  105.         }
  106.         public E delete(SLLNode<E> node) {
  107.             if (first != null) {
  108.                 SLLNode<E> tmp = first;
  109.                 if (first == node) {
  110.                     return this.deleteFirst();
  111.                 }
  112.                 while (tmp.succ != node && tmp.succ.succ != null)
  113.                     tmp = tmp.succ;
  114.                 if (tmp.succ == node) {
  115.                     tmp.succ = tmp.succ.succ;
  116.                     return node.element;
  117.                 } else {
  118.                     System.out.println("Elementot ne postoi vo listata");
  119.                     return null;
  120.                 }
  121.             } else {
  122.                 System.out.println("Listata e prazna");
  123.                 return null;
  124.             }
  125.         }
  126.         public SLLNode<E> getFirst() {
  127.             return first;
  128.         }
  129.         public SLLNode<E> find(E o) {
  130.             if (first != null) {
  131.                 SLLNode<E> tmp = first;
  132.                 while (tmp.element != o && tmp.succ != null)
  133.                     tmp = tmp.succ;
  134.                 if (tmp.element == o) {
  135.                     return tmp;
  136.                 } else {
  137.                     System.out.println("Elementot ne postoi vo listata");
  138.                 }
  139.             } else {
  140.                 System.out.println("Listata e prazna");
  141.             }
  142.             return first;
  143.         }
  144.         public Iterator<E> iterator() {
  145.             // Return an iterator that visits all elements of this list, in left-to-right order.
  146.             return new LRIterator<E>();
  147.         } // //////////Inner class ////////////
  148.         private class LRIterator<E> implements Iterator<E> {
  149.             private SLLNode<E> place, curr;
  150.             private LRIterator() {
  151.                 place = (SLLNode<E>) first;
  152.                 curr = null;
  153.             }
  154.             public boolean hasNext() {
  155.                 return (place != null);
  156.             }
  157.             public E next() {
  158.                 if (place == null)
  159.                     throw new NoSuchElementException();
  160.                 E nextElem = place.element;
  161.                 curr = place;
  162.                 place = place.succ;
  163.                 return nextElem;
  164.             }
  165.         }
  166.         public void mirror() {
  167.             if (first != null) {
  168.                 //m=nextsucc, p=tmp,q=next
  169.                 SLLNode<E> tmp = first;
  170.                 SLLNode<E> newsucc = null;
  171.                 SLLNode<E> next;
  172.                 while (tmp != null) {
  173.                     next = tmp.succ;
  174.                     tmp.succ = newsucc;
  175.                     newsucc = tmp;
  176.                     tmp = next;
  177.                 }
  178.                 first = newsucc;
  179.             }
  180.         }
  181.         public void merge(SLL<E> in) {
  182.             if (first != null) {
  183.                 SLLNode<E> tmp = first;
  184.                 while (tmp.succ != null)
  185.                     tmp = tmp.succ;
  186.                 tmp.succ = in.getFirst();
  187.             } else {
  188.                 first = in.getFirst();
  189.             }
  190.         }
  191.         public void reverse(SLL list) {
  192.             SLLNode<E> counter = list.getFirst();
  193.             SLL<E> novo = new SLL<>();
  194.             while (counter != null) {
  195.                 novo.insertLast((E) counter);
  196.                 counter = counter.succ;
  197.             }
  198.             counter = novo.getFirst();
  199.             while (counter != null) {
  200.                 System.out.print(counter + " -> ");
  201.                 counter = counter.succ;
  202.             }
  203.         }
  204.         public SLL sortiraj() {
  205.             SLLNode<E> temp = getFirst();//        node temp = head;
  206.             while (temp != null) {  // Traverse the List
  207.                 SLLNode<E> min = temp; //        node min = temp;
  208.                 SLLNode<E> r = temp.succ;//   node r = temp.next;
  209.                 // Traverse the unsorted sublist
  210.                 while (r != null) {
  211.                     if ((int) min.element > (int) r.element) //  if (min.data > r.data)
  212.                         min = r;
  213.                     r = r.succ; //r = r.next;
  214.                 }
  215.                 // Swap Data
  216.                 E x = temp.element; //int x = temp.data;
  217.                 temp.element = min.element; //temp.data = min.data;
  218.                 min.element = x; //min.data = x;
  219.                 temp = temp.succ; //temp = temp.next;
  220.             }
  221.             return this;
  222.         }
  223.         public void removeDuplicates() {
  224.             SLLNode<E> amanvise = getFirst();
  225.             while(amanvise.succ!=null) {
  226.                 if(amanvise.element == amanvise.succ.element)
  227.                     delete(amanvise);
  228.                 amanvise = amanvise.succ;
  229.             }
  230.         }
  231.         public SLL specialJoin(SLL list1, SLL list2) {
  232.             SLLNode<E> counter1 = list1.getFirst();
  233.             SLLNode<E> counter2 = list2.getFirst();
  234.             SLL<E> nova = new SLL<>();
  235.             int help = 0;
  236.             while(counter1!=null && counter1.succ!=null && counter2!=null && counter2.succ!=null) {
  237.                     nova.insertLast((E) counter1); //first two from first list
  238.                     nova.insertLast((E) counter1.succ); //first two from first list
  239.                     nova.insertLast((E) counter2); //then following those, two from the second list
  240.                     nova.insertLast((E) counter2.succ);
  241.                     counter1 = counter1.succ.succ;
  242.                     counter2 = counter2.succ.succ; // counters go brr
  243.             }
  244.         if(counter1!=null) {
  245.                 while(counter1!=null) {
  246.                 nova.insertLast((E) counter1);
  247.                 counter1 = counter1.succ;
  248.             }
  249.         }
  250.         if(counter2!=null)
  251.             while(counter2!=null) {
  252.                 nova.insertLast((E) counter2);
  253.                 counter2 = counter2.succ;
  254.         }
  255.             return nova;
  256.         }
  257.     }
  258.  
  259. public class SpecialSLLJoin {
  260.     public static void main(String[] args) throws IOException{
  261.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  262.                 System.in));
  263.         String s = stdin.readLine();
  264.         int N = Integer.parseInt(s);
  265.         s = stdin.readLine();
  266.         SLL lista1 = new SLL();
  267.         SLL lista2 = new SLL();
  268.         String[] pomniza = s.split(" ");
  269.         for (int i = 0; i < N; i++) {
  270.             lista1.insertLast(Integer.parseInt(pomniza[i]));
  271.         }
  272.         s = stdin.readLine();
  273.         N = Integer.parseInt(s);
  274.         s = stdin.readLine();
  275.         pomniza = s.split(" ");
  276.         for (int i = 0; i < N; i++) {
  277.             lista2.insertLast(Integer.parseInt(pomniza[i]));
  278.         }
  279.         SLL spoeni = new SLL();
  280.         spoeni = spoeni.specialJoin(lista1,lista2);
  281.         SLLNode print = spoeni.getFirst();
  282.         while(print!=null) {
  283.             System.out.print(print + " -> ");
  284.             print = print.succ;
  285.         }
  286.     }
  287. }
  288. /*
  289. Дадени се две еднострано поврзани листи чии што јазли содржат по еден природен број.
  290. Треба да се спојат двете листи во една резултантна на тој начин што наизменично прво
  291. ќе се додаваат првите два јазли од првата листа во резултантната, па првите два од
  292. втората листа, па следните два од првата, па следните два од втората итн. Јазлите
  293. што ќе останат треба да се додадат на крај во резултантната листа, прво оние што
  294. останале од првата листа, потоа оние што останале од втората листа.
  295. Во првиот ред од влезот се дадени броевите од кои се составени јазлите по редослед
  296. во првата листа, а во вториот ред броевите од кои се составени јазлите по редослед
  297. во втората листа. На излез треба да се испечатат јазлите по редослед во резултантната споена листа.
  298.  
  299. Забелешка: Да се креира податочна структура еднострано поврзана листа и истата да се искористи во задачата.
  300. */
  301.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement