Advertisement
StefanTodorovski

[ADS/АПС] Lab 1.3: Special Join List / Спој листи наизменичн

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