Filip_Markoski

1.Lab1.2 Join Lists (Solved)

Oct 18th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.31 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.         while (firstOne != null && firstTwo != null) {
  136.             //&& firstOne.succ != null && firstTwo.succ != null
  137.            
  138.             if (firstOne.element.compareTo(firstTwo.element) < 0) {
  139.                 result.insertLast(firstOne.element);
  140.                 firstOne = firstOne.succ;
  141.             } else {
  142.                 result.insertLast(firstTwo.element);
  143.                 firstTwo = firstTwo.succ;
  144.             }
  145.         }
  146.         if (firstOne != null) {
  147.             while (firstOne != null) {
  148.                 result.insertLast(firstOne.element);
  149.                 firstOne = firstOne.succ;
  150.             }
  151.         }
  152.         if (firstTwo != null) {
  153.             while (firstTwo != null) {
  154.                 result.insertLast(firstTwo.element);
  155.                 firstTwo = firstTwo.succ;
  156.             }
  157.         }
  158.         return result;
  159.     }
  160.  
  161.     public E delete(SLLNode<E> node) {
  162.         if (first != null) {
  163.             SLLNode<E> tmp = first;
  164.             while (tmp.succ != node && tmp.succ.succ != null)
  165.                 tmp = tmp.succ;
  166.             if (tmp.succ == node) {
  167.                 tmp.succ = tmp.succ.succ;
  168.                 return node.element;
  169.             }
  170.             // else throw Exception;
  171.         }
  172.         return null;
  173.         // else throw Exception;
  174.     }
  175.  
  176.     public SLL<E> deleteDuplicates(SLL<E> one) {
  177.         SLL<E> result = new SLL<E>();
  178.         SLLNode<E> firstOne = one.getFirst();
  179.         while (firstOne != null
  180.                 && firstOne.succ != null) {
  181.             if (firstOne.element.compareTo(firstOne.succ.element) == 0) {
  182.                 firstOne = firstOne.succ;
  183.             } else {
  184.                 result.insertLast(firstOne.element);
  185.                 firstOne = firstOne.succ;
  186.             }
  187.            
  188.             if (firstOne.succ == null) {
  189.                 result.insertLast(firstOne.element);
  190.             }
  191.         }
  192.         return result;
  193.     }
  194.  
  195.     public Iterator<E> iterator() {
  196.         // Return an iterator that visits all elements of this list, in
  197.         // left-to-right order.
  198.         return new LRIterator<E>();
  199.     }
  200.  
  201.     private class LRIterator<E extends Comparable<E>> implements Iterator<E> {
  202.  
  203.         private SLLNode<E> place, prev, curr;
  204.  
  205.         @SuppressWarnings("unchecked")
  206.         private LRIterator() {
  207.             place = (SLLNode<E>) first;
  208.             curr = prev = null;
  209.         }
  210.  
  211.         public boolean hasNext() {
  212.             return (place != null);
  213.         }
  214.  
  215.         public E next() {
  216.             if (place == null) {
  217.                 throw new NoSuchElementException();
  218.             }
  219.             E nextElem = place.element;
  220.             prev = curr;
  221.             curr = place;
  222.             place = place.succ;
  223.             return nextElem;
  224.         }
  225.  
  226.         public void remove() {
  227.             // Not implemented
  228.         }
  229.  
  230.     }
  231. }
  232.  
  233. public class SLLJoinLists<E extends Comparable<E>> {
  234.  
  235.     public static void main(String[] args) throws IOException {
  236.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  237.         String s = stdin.readLine();
  238.         int N = Integer.parseInt(s);
  239.         s = stdin.readLine();
  240.         String[] pomniza = s.split(" ");
  241.  
  242.         SLL<Integer> listOne = new SLL<Integer>();
  243.         SLL<Integer> listTwo = new SLL<Integer>();
  244.         for (int i = 0; i < N; i++) {
  245.             listOne.insertLast(Integer.parseInt(pomniza[i]));
  246.         }
  247.  
  248.         //System.out.println(listOne.toString());
  249.  
  250.         s = stdin.readLine();
  251.         N = Integer.parseInt(s);
  252.         s = stdin.readLine();
  253.         pomniza = s.split(" ");
  254.         for (int i = 0; i < N; i++) {
  255.             listTwo.insertLast(Integer.parseInt(pomniza[i]));
  256.         }
  257.  
  258.         //System.out.println(listTwo.toString());
  259.  
  260.         SLL<Integer> listJoined = listOne.joinLists(listOne, listTwo);
  261.         //System.out.println(listJoined.toString());
  262.        
  263.        
  264.         listJoined = listJoined.deleteDuplicates(listJoined);
  265.         System.out.println(listJoined.toString());
  266.        
  267.         /*
  268.          * SLL<Integer> listJoined = listOne.joinLists(listTwo); Iterator<Integer> it =
  269.          * listJoined.iterator(); while (it.hasNext()) { System.out.print(it.next());
  270.          * if(it.hasNext()) System.out.print(" "); } System.out.println();
  271.          */
  272.     }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment