Martina312

Спој листи

Feb 3rd, 2020
80
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 SLLJoinLists {
  4.     public static void removeDuplicates(SLL<Integer> list){
  5.         SLLNode<Integer> tmp=list.getFirst();
  6.         SLLNode<Integer> next=null;
  7.  
  8.         while(tmp!=null){
  9.             next=tmp.succ;
  10.             if(next==null)
  11.                 break;
  12.             if(tmp.element==next.element){
  13.                 list.delete(tmp);
  14.             }
  15.             tmp=next;
  16.         }
  17.         System.out.println(list);
  18.     }
  19.     public static void join(SLL<Integer> list1, SLL<Integer> list2){
  20.         SLLNode<Integer> tmp1=list1.getFirst();
  21.         SLLNode<Integer> tmp2=list2.getFirst();
  22.  
  23.         SLL<Integer> result=new SLL<>();
  24.  
  25.         while(tmp1!=null && tmp2!=null){
  26.             if (tmp1.element<=tmp2.element){
  27.                 result.insertLast(tmp1.element);
  28.                 tmp1=tmp1.succ;
  29.             }
  30.             else{
  31.                 result.insertLast(tmp2.element);
  32.                 tmp2=tmp2.succ;
  33.             }
  34.         }
  35.         if(tmp1!=null){
  36.             while(tmp1!=null){
  37.                 result.insertLast(tmp1.element);
  38.                 tmp1=tmp1.succ;
  39.             }
  40.         }
  41.         if(tmp2!=null){
  42.             while(tmp2!=null){
  43.                 result.insertLast(tmp2.element);
  44.                 tmp2=tmp2.succ;
  45.             }
  46.         }
  47.         System.out.println(result);
  48.         removeDuplicates(result);
  49.     }
  50.     public static void main(String[] args) {
  51.         Scanner in=new Scanner(System.in);
  52.  
  53.         int n1=in.nextInt();
  54.         SLL<Integer> list1=new SLL<>();
  55.         for(int i=0;i<n1;i++){
  56.             list1.insertLast(in.nextInt());
  57.         }
  58.  
  59.         int n2=in.nextInt();
  60.         SLL<Integer> list2=new SLL<>();
  61.         for(int i=0;i<n2;i++){
  62.             list2.insertLast(in.nextInt());
  63.         }
  64.  
  65.         join(list1,list2);
  66.     }
  67. }
Add Comment
Please, Sign In to add comment