Advertisement
StefanTodorovski

[ADS/АПС] Lab 1.2: Join lists / Спој листи

Oct 23rd, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. /*
  2. Join lists Problem 2 (0 / 0)
  3.  
  4. For given two single linked lists with integers sorted in ascending order, join them in a sorted single linked list in ascending order. Duplicate nodes should be deleted.
  5.  
  6. The first line in the input gives the first list node number, then in the second line there are the node numbers in the first list. The third line gives the second list node number, and in the end the forth line gives the numbers from the second list. The output prints the node numbers of the resulting list.
  7.  
  8. Class (Java): SLLJoinLists
  9.  
  10. Note: Create data structure single linked list and use it in this problem.
  11. */
  12.  
  13. import java.io.BufferedReader;
  14. import java.io.IOException;
  15. import java.io.InputStreamReader;
  16. import java.util.Iterator;
  17. import java.util.NoSuchElementException;
  18.  
  19. class SLLNode<E> {
  20.     public E elem;
  21.     public SLLNode<E> succ;
  22.    
  23.     public SLLNode() {
  24.         this.elem = null;
  25.         this.succ = null;
  26.     }
  27.    
  28.     public SLLNode(E elem, SLLNode<E> succ) {
  29.         this.elem = elem;
  30.         this.succ = succ;
  31.     }
  32. }
  33.  
  34. class SLL<E extends Comparable <E>> {
  35.     private SLLNode<E> first;
  36.  
  37.     public SLL() { this.first = null; }
  38.    
  39.     public SLL(SLLNode<E> first) {
  40.         this.first = first;
  41.     }
  42.    
  43.     public void insertFirst(E elem) {
  44.         SLLNode<E> newNode = new SLLNode<E>(elem, null);
  45.         if(this.first == null) this.first = newNode;
  46.         else {
  47.             newNode.succ = this.first;
  48.             this.first = newNode;
  49.         }
  50.     }
  51.    
  52.     public void insertLast(E elem) {
  53.         if(this.first == null) {
  54.             insertFirst(elem);
  55.             return;
  56.         }
  57.         SLLNode<E> curr = this.first;
  58.         while(curr.succ != null) {
  59.             curr = curr.succ;
  60.         }
  61.         SLLNode<E> newNode = new SLLNode<E>(elem, null);
  62.         curr.succ = newNode;
  63.     }
  64.    
  65.     public Iterator<E> iterator() {
  66.         return new LRIterator<E>();
  67.     }
  68.    
  69.     private class LRIterator<E> implements Iterator<E> {
  70.         private SLLNode<E> place;
  71.        
  72.         private LRIterator() {
  73.             place = (SLLNode<E>) first;
  74.         }
  75.        
  76.         public boolean hasNext() {
  77.             return place != null;
  78.         }
  79.        
  80.         public E next() {
  81.             if(place == null)
  82.                 throw new NoSuchElementException();
  83.             E nextElement = place.elem;
  84.             place = place.succ;
  85.             return nextElement;
  86.         }
  87.     }
  88.    
  89.     public void deleteSuccNode(SLLNode<E> node) {
  90.         if(first == null || node.succ == null) return;
  91.         node.succ = node.succ.succ;
  92.     }
  93.    
  94.     public SLL<E> removeDuplicates(SLL<E> lista) {
  95.         SLLNode<E> curr = lista.first;
  96.         while(curr.succ != null) {
  97.             if(curr.elem.compareTo(curr.succ.elem) == 0) {
  98.                 deleteSuccNode(curr);
  99.             } else curr = curr.succ;
  100.         }
  101.         return lista;
  102.     }
  103.    
  104.     public SLL<E> joinLists(SLL<E> lista) {
  105.         SLL<E> finalList = new SLL<E>();
  106.         SLLNode<E> curr1 = this.first;
  107.         SLLNode<E> curr2 = lista.first;
  108.        
  109.         while(curr1 != null && curr2 != null) {
  110.             if(curr1.elem.compareTo(curr2.elem) < 0) {
  111.                 finalList.insertLast(curr1.elem);
  112.                 curr1 = curr1.succ;
  113.             } else {
  114.                 finalList.insertLast(curr2.elem);
  115.                 curr2 = curr2.succ;
  116.             }
  117.         }
  118.  
  119.         while(curr1 != null) {
  120.             finalList.insertLast(curr1.elem);
  121.             curr1 = curr1.succ;
  122.         }
  123.        
  124.         while(curr2 != null) {
  125.             finalList.insertLast(curr2.elem);
  126.             curr2 = curr2.succ;
  127.         }
  128.        
  129.         return removeDuplicates(finalList);
  130.     }
  131. }
  132.  
  133.  
  134. public class SLLJoinLists {
  135.     public static void main(String[] args) throws IOException {
  136.  
  137.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  138.         String s = stdin.readLine();
  139.         int N = Integer.parseInt(s);
  140.         s = stdin.readLine();
  141.         String[] pomniza = s.split(" ");
  142.        
  143.         SLL<Integer> lista1 = new SLL<Integer>();
  144.         SLL<Integer> lista2 = new SLL<Integer>();
  145.         SLL<Integer> spoeni = new SLL<Integer>();
  146.        
  147.         for (int i = 0; i < N; i++) {
  148.             lista1.insertLast(Integer.parseInt(pomniza[i]));
  149.         }
  150.  
  151.         s = stdin.readLine();
  152.         N = Integer.parseInt(s);
  153.         s = stdin.readLine();
  154.         pomniza = s.split(" ");
  155.         for (int i = 0; i < N; i++) {
  156.             lista2.insertLast(Integer.parseInt(pomniza[i]));
  157.         }
  158.  
  159.         spoeni = lista1.joinLists(lista2);
  160.         Iterator<Integer> it = spoeni.iterator();
  161.         while (it.hasNext()) {
  162.             System.out.print(it.next());
  163.             if(it.hasNext())
  164.                 System.out.print(" ");
  165.         }
  166.         System.out.println();
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement