Advertisement
JStefan

APS_LAB1_Task3

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