Advertisement
FahimFaisal

220_new_RAK

Feb 16th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.03 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package Array_List_Circular;
  7.  
  8. import java.util.HashSet;
  9.  
  10. /**
  11.  *
  12.  * @author acer
  13.  */
  14. public class Tester {
  15.    
  16.     public static void main (String [] args){
  17.        
  18.         int arr1[] = {7,8,1, 2, 2, 2, 3,4,4};
  19.         int arr2[] = {6,7,1,2, 3, 4, 5};
  20.         int m = arr1.length;
  21.         int n = arr2.length;
  22.          
  23.          System.out.println("\n------------Cir array intersection--------------");
  24.         int [] unified=Intersection(arr1, arr2,2,2);
  25.         for(int i:unified){
  26.             System.out.print(i+" ");
  27.         }
  28.          System.out.println("\n-----------Cir array Union-------------------");
  29.         int [] unified3=Union(arr1, arr2,2,2);
  30.         for(int i:unified3){
  31.             System.out.print(i+" ");
  32.         }
  33.        
  34.         int arr3[] = {1, 2, 2, 2, 3,4,4};
  35.         int arr4[] = {1,2, 3, 4, 5};
  36.         System.out.println("");
  37.         System.out.println("\n------------Linear array Union---------------");
  38.         int [] unified2=Union(arr3, arr4);
  39.         for(int i:unified2){
  40.             System.out.print(i+" ");
  41.         }
  42.        
  43.          System.out.println("\n------------Linear array Union---------------");
  44.         int [] unified4=Intersection(arr3, arr4);
  45.         for(int i:unified4){
  46.             System.out.print(i+" ");
  47.         }
  48.        
  49.        
  50.        
  51.         //boolean b=check(C,2,1);
  52.         //System.out.println(b);
  53.         System.out.println("\n/////  Test 01  /////");
  54.     Object [] a1 = {10,10,20,30,40};
  55.     System.out.println();
  56.     LinkedList h1 = new LinkedList(a1); // Creates a linked list using the values from the array
  57.     // head will refer to the Node that contains the element from a[0]
  58.    
  59.     h1.printList(); // This should print: 10,20,30,40.
  60.  
  61.     Object [] a2 = {30,30,40,50,60,70};
  62.     System.out.println();
  63.     LinkedList h2 = new LinkedList(a2); // Creates a linked list using the values from the array
  64.     // head will refer to the Node that contains the element from a[0]
  65.    
  66.     h2.printList(); // This should print: 10,20,30,40.
  67.    
  68.     //nodeIntersection(h1.head,h2.head);
  69.     Node h3=Tester.nodeUnion(h1.head, h2.head);
  70.     LinkedList h4 = new LinkedList(h3);
  71.     h4.printList();
  72.    
  73.     }
  74.    
  75.     static int[] Union(int arr1[], int arr2[])
  76.     {
  77.         int m=arr1.length;
  78.         int n=arr2.length;
  79.         int []C=new int[m+n];
  80.         int k=0;
  81.       int i = 0, j = 0;
  82.       while (i < m && j < n)
  83.       {
  84.         if (arr1[i] < arr2[j])
  85.         {
  86.             if(check(C,arr1[i],k)){
  87.             C[k]=arr1[i];
  88.             k++;
  89.             i++;
  90.         }else{
  91.             i++;    
  92.             }
  93.         }
  94.           //System.out.print(arr1[i++]+" ");
  95.         else if (arr2[j] < arr1[i])
  96.             if(check(C,arr2[j],k)){
  97.             C[k]=arr2[j];
  98.             k++;
  99.             j++;
  100.         }else{
  101.             j++;    
  102.             }
  103.             //C[k++]=arr2[j++];
  104.            
  105.           //System.out.print(arr2[j++]+" ");
  106.         else
  107.         {
  108.             if(check(C,arr2[j],k)){
  109.           C[k++]=arr2[j++];
  110.             i++;
  111.             }
  112.             else{
  113.           j++;
  114.           //System.out.print(arr2[j++]+" ");
  115.           i++;
  116.             }
  117.          
  118.         }
  119.       }
  120.        
  121.       /* Print remaining elements of  
  122.          the larger array */
  123.       while(i < m)
  124.            
  125.           C[k++]=arr1[i++];
  126.          
  127.        //System.out.print(arr1[i++]+" ");
  128.       while(j < n)
  129.        
  130.           C[k++]=arr2[j++];
  131.          
  132.        //System.out.print(arr2[j++]+" ");
  133.          
  134.       //return 0;    
  135.       int [] newArr=new int[k];
  136.       for(int l=0;l<newArr.length;l++){
  137.       newArr[l]=C[l];
  138.       }
  139.       return newArr;
  140.     }
  141.    
  142.    
  143.    
  144.    static int [] Intersection(int arr1[], int arr2[])
  145.     {
  146.         int m=arr1.length;
  147.         int n=arr2.length;
  148.        
  149.    
  150.         int [] C=new int[m+n];
  151.       int i = 0, j = 0,k=0;
  152.       while (i < m && j < n)
  153.       {
  154.         if (arr1[i] < arr2[j])
  155.           i++;
  156.         else if (arr2[j] < arr1[i])
  157.           j++;
  158.         else
  159.         {
  160.           //System.out.print(arr2[j++]+" ");
  161.             C[k]=arr2[j++];
  162.             k++;
  163.           i++;
  164.         }
  165.       }
  166.       int [] intersect=new int [k];
  167.       for(int l=0;l<intersect.length;l++){
  168.           intersect[l]=C[l];
  169.       }
  170.       return intersect;
  171.     }
  172.    
  173.     static int [] Intersection(int arr1[], int arr2[],int st1,int st2)
  174.     {
  175.         int m=arr1.length;
  176.         int n=arr2.length;
  177.        
  178.    
  179.         int [] C=new int[m+n];
  180.         int i1=0,j1=0;
  181.       int i = st1, j = st2,k=0;
  182.       while (i1 < m && j1 < n)
  183.       {
  184.         if (arr1[i] < arr2[j]) {
  185.           i=(i+1)%m;
  186.         i1++;
  187.         }
  188.         else if (arr2[j] < arr1[i]) {
  189.           j=(j+1)%n;
  190.                 j1++;
  191.                 }
  192.         else
  193.         {
  194.           //System.out.print(arr2[j++]+" ");
  195.             C[k]=arr2[j++%m];
  196.             k++;
  197.           i=(i+1)%m;
  198.           i1++;
  199.           j1++;
  200.         }
  201.       }
  202.       int [] intersect=new int [k];
  203.       for(int l=0;l<intersect.length;l++){
  204.           intersect[l]=C[l];
  205.       }
  206.       return intersect;
  207.     }
  208.      
  209.     static int[] Union(int arr1[], int arr2[],int st1,int st2)
  210.     {
  211.         int m=arr1.length;
  212.         int n=arr2.length;
  213.         int []C=new int[m+n];
  214.         int k=0;
  215.         int i1=0,j1=0;
  216.       int i = st1, j = st2;
  217.       while (i1 < m && j1 < n)
  218.       {
  219.         if (arr1[i] < arr2[j])
  220.         {
  221.             if(check(C,arr1[i],k)){
  222.             C[k]=arr1[i];
  223.             k++;
  224.            
  225.             i1++;
  226.         }else{
  227.            
  228.             i1++;
  229.             }
  230.             i=(i+1)%m;
  231.         }
  232.           //System.out.print(arr1[i++]+" ");
  233.         else if (arr2[j] < arr1[i]){
  234.             if(check(C,arr2[j],k)){
  235.             C[k]=arr2[j];
  236.             k++;
  237.            
  238.             j1++;
  239.         }else{
  240.            
  241.             j1++;
  242.             }
  243.             j=(j+1)%n;
  244.         }
  245.             //C[k++]=arr2[j++];
  246.            
  247.           //System.out.print(arr2[j++]+" ");
  248.         else
  249.         {
  250.             if(check(C,arr2[j],k)){
  251.           C[k++]=arr2[j++];
  252.            
  253.             i1++;
  254.             }
  255.             else{
  256.          
  257.           j1++;
  258.           //System.out.print(arr2[j++]+" ");
  259.          
  260.           i1++;
  261.             }
  262.            i=(i+1)%m;
  263.            j=(j+1)%n;
  264.          
  265.         }
  266.       }
  267.        
  268.       /* Print remaining elements of  
  269.          the larger array */
  270.       while(i1 < m){
  271.            
  272.           C[k++]=arr1[i++%m];
  273.           i1++;
  274.       }
  275.          
  276.        //System.out.print(arr1[i++]+" ");
  277.       while(j1 < n) {
  278.            
  279.           C[k++]=arr2[j++%n];
  280.           j1++;
  281.       }
  282.          
  283.        //System.out.print(arr2[j++]+" ");
  284.          
  285.       //return 0;    
  286.       int [] newArr=new int[k];
  287.       for(int l=0;l<newArr.length;l++){
  288.       newArr[l]=C[l];
  289.       }
  290.       return newArr;
  291.     }
  292.    
  293.     public static boolean check(int []A,int elem,int idx){
  294.        boolean b_C=true;
  295.         for(int i=0;i<idx;i++){
  296.            if(A[i]==elem){
  297.              b_C=false;
  298.              break;
  299.            }
  300.         }
  301.        
  302.            return b_C;
  303.     }
  304.    
  305.     public static Node nodeIntersection(Node h1, Node h2) {
  306.         Node head = new Node(h1.element, null);
  307.         Node tail = head;
  308.         for (Node n = h1.next; n != null; n = n.next) {
  309.             if (checkList(h2, n.element)) {
  310.                 if(!checkList(head,n.element)){
  311.                     Node mn = new Node(n.element, null);
  312.                     tail.next = mn;
  313.                     tail = mn;
  314.                 }
  315.             }
  316.         }
  317.         for (Node n = h2; n != null; n = n.next) {
  318.             if (checkList(h1, n.element)) {
  319.                 if(!checkList(head,n.element)){
  320.                     Node mn = new Node(n.element, null);
  321.                     tail.next = mn;
  322.                     tail = mn;
  323.                 }
  324.             }
  325.         }
  326.         return head;
  327.     }
  328.  
  329.     public static boolean checkList(Node h, Object elem) {
  330.         for (Node n = h; n != null; n = n.next) {
  331.             if (n.element.equals(elem)) {
  332.                 return true;
  333.             }
  334.         }
  335.         return false;
  336.     }
  337.    
  338.     public static Node nodeUnion(Node h1,Node h2){
  339.         Node head=new Node(h1.element,null);
  340.         Node tail=head;
  341.         for(Node n=h1.next;n!=null;n=n.next){
  342.             if(!checkList(head,n.element)){
  343.                 Node mn=new Node(n.element,null);
  344.                 tail.next=mn;
  345.                 tail=mn;
  346.             }
  347.         }
  348.         for(Node n=h2;n!=null;n=n.next){
  349.             if(!checkList(head,n.element)){
  350.                 Node mn=new Node(n.element,null);
  351.                 tail.next=mn;
  352.                 tail=mn;
  353.             }
  354.         }
  355.         return head;
  356.     }
  357.    
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement