Advertisement
Martina312

Intersection

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