Advertisement
JStefan

APS_LAB1_Task2

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