Filip_Markoski

1.Lab1.3 Special Join Lists (Solved)

Oct 18th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.33 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. class SLLNode<E extends Comparable<E>> {
  8.     protected E element;
  9.     protected SLLNode<E> succ;
  10.  
  11.     public SLLNode(E element, SLLNode<E> succ) {
  12.         this.element = element;
  13.         this.succ = succ;
  14.     }
  15.  
  16.     @Override
  17.     public String toString() {
  18.         return element.toString();
  19.     }
  20. }
  21.  
  22. class SLL<E extends Comparable<E>> {
  23.     private SLLNode<E> first;
  24.  
  25.     public SLL() {
  26.         this.first = null;
  27.     }
  28.  
  29.     public void deleteSLL() {
  30.         first = null;
  31.     }
  32.  
  33.     public int length() {
  34.         int len = 0;
  35.         if (first != null) {
  36.             SLLNode<E> temp = first;
  37.             len = 1;
  38.             while (temp.succ != null) {
  39.                 temp = temp.succ;
  40.                 len++;
  41.             }
  42.         }
  43.         return len;
  44.     }
  45.  
  46.     @Override
  47.     public String toString() {
  48.         if (first != null) {
  49.             SLLNode<E> temp = first;
  50.             StringBuffer sb = new StringBuffer();
  51.             sb.append(temp.toString() + " ");
  52.             while (temp.succ != null) {
  53.                 temp = temp.succ;
  54.                 sb.append(temp + " ");
  55.             }
  56.             return sb.toString();
  57.         } else {
  58.             return "The linked list is empty.";
  59.         }
  60.     }
  61.  
  62.     public void insertFirst(E elem) {
  63.         SLLNode<E> insert = new SLLNode<E>(elem, first);
  64.         /*
  65.          * insert is a node pointing to the head, so the link has already been
  66.          * established by the constructor
  67.          */
  68.         first = insert;
  69.     }
  70.  
  71.     public void insertLast(E elem) {
  72.         if (first != null) {
  73.             SLLNode<E> temp = first;
  74.             while (temp.succ != null) {
  75.                 temp = temp.succ;
  76.             }
  77.             SLLNode<E> insert = new SLLNode<E>(elem, null);
  78.             temp.succ = insert;
  79.         } else {
  80.             insertFirst(elem);
  81.         }
  82.     }
  83.  
  84.     public void insertBefore(E elem, SLLNode<E> before) {
  85.         if (first != null) {
  86.             if (first == before) {
  87.                 this.insertFirst(elem);
  88.                 return;
  89.             }
  90.             /* Search till temp is the before node minus one! */
  91.             SLLNode<E> temp = first;
  92.             while (temp.succ != before) {
  93.                 temp = temp.succ;
  94.             }
  95.             if (temp.succ == before) {
  96.                 SLLNode<E> insert = new SLLNode<E>(elem, before);
  97.                 temp.succ = insert;
  98.             } else {
  99.                 System.out.println("The element cannot be found in the list.");
  100.                 return;
  101.             }
  102.         } else {
  103.             System.out.println("The linked list is empty.");
  104.         }
  105.     }
  106.  
  107.     public void insertAfter(E elem, SLLNode<E> after) {
  108.         if (after != null) {
  109.             SLLNode<E> insert = new SLLNode<E>(elem, after.succ);
  110.             after.succ = insert;
  111.         } else {
  112.             System.out.println("The given node as a parameter is null.");
  113.         }
  114.     }
  115.  
  116.     public SLLNode<E> getFirst() {
  117.         return first;
  118.     }
  119.  
  120.     public void merge(SLL<E> in) {
  121.         if (first != null) {
  122.             SLLNode<E> tmp = first;
  123.             while (tmp.succ != null)
  124.                 tmp = tmp.succ;
  125.             tmp.succ = in.getFirst();
  126.         } else {
  127.             first = in.getFirst();
  128.         }
  129.     }
  130.  
  131.     public SLL<E> joinLists(SLL<E> one, SLL<E> two) {
  132.         SLL<E> result = new SLL<E>();
  133.         SLLNode<E> firstOne = one.getFirst();
  134.         SLLNode<E> firstTwo = two.getFirst();
  135.         boolean flag = true;
  136.         while (firstOne != null && firstTwo != null && firstOne.succ != null && firstTwo.succ != null) {
  137.            
  138.                 result.insertLast(firstOne.element);
  139.                 firstOne = firstOne.succ;
  140.                 result.insertLast(firstOne.element);
  141.                 firstOne = firstOne.succ;
  142.                
  143.                 result.insertLast(firstTwo.element);
  144.                 firstTwo = firstTwo.succ;
  145.                 result.insertLast(firstTwo.element);
  146.                 firstTwo = firstTwo.succ;
  147.                
  148.         }
  149.         if (firstOne != null) {
  150.             while (firstOne != null) {
  151.                 result.insertLast(firstOne.element);
  152.                 firstOne = firstOne.succ;
  153.             }
  154.         }
  155.         if (firstTwo != null) {
  156.             while (firstTwo != null) {
  157.                 result.insertLast(firstTwo.element);
  158.                 firstTwo = firstTwo.succ;
  159.             }
  160.         }
  161.         return result;
  162.     }
  163.  
  164.     public E delete(SLLNode<E> node) {
  165.         if (first != null) {
  166.             SLLNode<E> tmp = first;
  167.             while (tmp.succ != node && tmp.succ.succ != null)
  168.                 tmp = tmp.succ;
  169.             if (tmp.succ == node) {
  170.                 tmp.succ = tmp.succ.succ;
  171.                 return node.element;
  172.             }
  173.             // else throw Exception;
  174.         }
  175.         return null;
  176.         // else throw Exception;
  177.     }
  178.  
  179.     public SLL<E> deleteDuplicates(SLL<E> one) {
  180.         SLL<E> result = new SLL<E>();
  181.         SLLNode<E> firstOne = one.getFirst();
  182.         while (firstOne != null
  183.                 && firstOne.succ != null) {
  184.             if (firstOne.element.compareTo(firstOne.succ.element) == 0) {
  185.                 firstOne = firstOne.succ;
  186.             } else {
  187.                 result.insertLast(firstOne.element);
  188.                 firstOne = firstOne.succ;
  189.             }
  190.            
  191.             if (firstOne.succ == null) {
  192.                 result.insertLast(firstOne.element);
  193.             }
  194.         }
  195.         return result;
  196.     }
  197.  
  198.     public Iterator<E> iterator() {
  199.         // Return an iterator that visits all elements of this list, in
  200.         // left-to-right order.
  201.         return new LRIterator<E>();
  202.     }
  203.  
  204.     private class LRIterator<E extends Comparable<E>> implements Iterator<E> {
  205.  
  206.         private SLLNode<E> place, prev, curr;
  207.  
  208.         @SuppressWarnings("unchecked")
  209.         private LRIterator() {
  210.             place = (SLLNode<E>) first;
  211.             curr = prev = null;
  212.         }
  213.  
  214.         public boolean hasNext() {
  215.             return (place != null);
  216.         }
  217.  
  218.         public E next() {
  219.             if (place == null) {
  220.                 throw new NoSuchElementException();
  221.             }
  222.             E nextElem = place.element;
  223.             prev = curr;
  224.             curr = place;
  225.             place = place.succ;
  226.             return nextElem;
  227.         }
  228.  
  229.         public void remove() {
  230.             // Not implemented
  231.         }
  232.  
  233.     }
  234. }
  235.  
  236. public class SpecialSLLJoin<E extends Comparable<E>> {
  237.  
  238.     public static void main(String[] args) throws IOException {
  239.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  240.         String s = stdin.readLine();
  241.         int N = Integer.parseInt(s);
  242.         s = stdin.readLine();
  243.         String[] pomniza = s.split(" ");
  244.  
  245.         SLL<Integer> listOne = new SLL<Integer>();
  246.         SLL<Integer> listTwo = new SLL<Integer>();
  247.         for (int i = 0; i < N; i++) {
  248.             listOne.insertLast(Integer.parseInt(pomniza[i]));
  249.         }
  250.  
  251.         //System.out.println(listOne.toString());
  252.  
  253.         s = stdin.readLine();
  254.         N = Integer.parseInt(s);
  255.         s = stdin.readLine();
  256.         pomniza = s.split(" ");
  257.         for (int i = 0; i < N; i++) {
  258.             listTwo.insertLast(Integer.parseInt(pomniza[i]));
  259.         }
  260.  
  261.         //System.out.println(listTwo.toString());
  262.  
  263.         SLL<Integer> listJoined = listOne.joinLists(listOne, listTwo);
  264.         System.out.println(listJoined.toString());
  265.        
  266.         /*
  267.          * SLL<Integer> listJoined = listOne.joinLists(listTwo); Iterator<Integer> it =
  268.          * listJoined.iterator(); while (it.hasNext()) { System.out.print(it.next());
  269.          * if(it.hasNext()) System.out.print(" "); } System.out.println();
  270.          */
  271.     }
  272. }
Add Comment
Please, Sign In to add comment