Advertisement
Martina312

sort

Feb 12th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SortiranjeDve {
  4.     private static void transform(DLL<Integer> list1, DLL<Integer> list2) {
  5.         DLLNode<Integer> tmp1=list1.getFirst();
  6.         DLLNode<Integer> tmp2=list2.getFirst();
  7.  
  8.         while (tmp1!=null && tmp2!=null){
  9.             if(tmp1.element<tmp2.element){
  10.                 while (tmp1!=null){
  11.                     if(tmp1.element<tmp2.element){
  12.                         tmp1=tmp1.succ;
  13.                         if(tmp1.succ==null) break;
  14.                     }else{
  15.                         break;
  16.                     }
  17.                 }
  18.                  if(tmp1.succ==null){
  19.                      if(tmp1.element>tmp2.element){
  20.                          list1.insertBefore(tmp2.element,tmp1);
  21.                      }
  22.                      tmp1=tmp1.succ;
  23.                      tmp2=tmp2.succ;
  24.                  }else {
  25.                      list1.insertBefore(tmp2.element, tmp1);
  26.                      tmp2 = tmp2.succ;
  27.                  }
  28.             }
  29.             else{
  30.                 list1.insertBefore(tmp2.element, tmp1);
  31.                 tmp1=tmp1.succ;
  32.                 tmp2=tmp2.succ;
  33.             }
  34.  
  35.         }
  36.         if(tmp2!=null && tmp1==null) {
  37.             while (tmp2 != null) {
  38.                 list1.insertLast(tmp2.element);
  39.                 tmp2 = tmp2.succ;
  40.             }
  41.         }
  42.         System.out.println(list1);
  43.     }
  44.     public static void main(String[] args) {
  45.         Scanner in=new Scanner(System.in);
  46.         int n=in.nextInt();
  47.  
  48.         DLL<Integer> list1=new DLL<>();
  49.         for(int i=0;i<n;i++){
  50.             list1.insertLast(in.nextInt());
  51.         }
  52.  
  53.         int n2=in.nextInt();
  54.         DLL<Integer> list2=new DLL<>();
  55.         for(int i=0;i<n2;i++){
  56.             list2.insertLast(in.nextInt());
  57.         }
  58.         transform(list1,list2);
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement