Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package Array_List_Circular;
- import java.util.HashSet;
- /**
- *
- * @author acer
- */
- public class Tester {
- public static void main (String [] args){
- int arr1[] = {7,8,1, 2, 2, 2, 3,4,4};
- int arr2[] = {6,7,1,2, 3, 4, 5};
- int m = arr1.length;
- int n = arr2.length;
- System.out.println("\n------------Cir array intersection--------------");
- int [] unified=Intersection(arr1, arr2,2,2);
- for(int i:unified){
- System.out.print(i+" ");
- }
- System.out.println("\n-----------Cir array Union-------------------");
- int [] unified3=Union(arr1, arr2,2,2);
- for(int i:unified3){
- System.out.print(i+" ");
- }
- int arr3[] = {1, 2, 2, 2, 3,4,4};
- int arr4[] = {1,2, 3, 4, 5};
- System.out.println("");
- System.out.println("\n------------Linear array Union---------------");
- int [] unified2=Union(arr3, arr4);
- for(int i:unified2){
- System.out.print(i+" ");
- }
- System.out.println("\n------------Linear array Union---------------");
- int [] unified4=Intersection(arr3, arr4);
- for(int i:unified4){
- System.out.print(i+" ");
- }
- //boolean b=check(C,2,1);
- //System.out.println(b);
- System.out.println("\n///// Test 01 /////");
- Object [] a1 = {10,10,20,30,40};
- System.out.println();
- LinkedList h1 = new LinkedList(a1); // Creates a linked list using the values from the array
- // head will refer to the Node that contains the element from a[0]
- h1.printList(); // This should print: 10,20,30,40.
- Object [] a2 = {30,30,40,50,60,70};
- System.out.println();
- LinkedList h2 = new LinkedList(a2); // Creates a linked list using the values from the array
- // head will refer to the Node that contains the element from a[0]
- h2.printList(); // This should print: 10,20,30,40.
- //nodeIntersection(h1.head,h2.head);
- Node h3=Tester.nodeUnion(h1.head, h2.head);
- LinkedList h4 = new LinkedList(h3);
- h4.printList();
- }
- static int[] Union(int arr1[], int arr2[])
- {
- int m=arr1.length;
- int n=arr2.length;
- int []C=new int[m+n];
- int k=0;
- int i = 0, j = 0;
- while (i < m && j < n)
- {
- if (arr1[i] < arr2[j])
- {
- if(check(C,arr1[i],k)){
- C[k]=arr1[i];
- k++;
- i++;
- }else{
- i++;
- }
- }
- //System.out.print(arr1[i++]+" ");
- else if (arr2[j] < arr1[i])
- if(check(C,arr2[j],k)){
- C[k]=arr2[j];
- k++;
- j++;
- }else{
- j++;
- }
- //C[k++]=arr2[j++];
- //System.out.print(arr2[j++]+" ");
- else
- {
- if(check(C,arr2[j],k)){
- C[k++]=arr2[j++];
- i++;
- }
- else{
- j++;
- //System.out.print(arr2[j++]+" ");
- i++;
- }
- }
- }
- /* Print remaining elements of
- the larger array */
- while(i < m)
- C[k++]=arr1[i++];
- //System.out.print(arr1[i++]+" ");
- while(j < n)
- C[k++]=arr2[j++];
- //System.out.print(arr2[j++]+" ");
- //return 0;
- int [] newArr=new int[k];
- for(int l=0;l<newArr.length;l++){
- newArr[l]=C[l];
- }
- return newArr;
- }
- static int [] Intersection(int arr1[], int arr2[])
- {
- int m=arr1.length;
- int n=arr2.length;
- int [] C=new int[m+n];
- int i = 0, j = 0,k=0;
- while (i < m && j < n)
- {
- if (arr1[i] < arr2[j])
- i++;
- else if (arr2[j] < arr1[i])
- j++;
- else
- {
- //System.out.print(arr2[j++]+" ");
- C[k]=arr2[j++];
- k++;
- i++;
- }
- }
- int [] intersect=new int [k];
- for(int l=0;l<intersect.length;l++){
- intersect[l]=C[l];
- }
- return intersect;
- }
- static int [] Intersection(int arr1[], int arr2[],int st1,int st2)
- {
- int m=arr1.length;
- int n=arr2.length;
- int [] C=new int[m+n];
- int i1=0,j1=0;
- int i = st1, j = st2,k=0;
- while (i1 < m && j1 < n)
- {
- if (arr1[i] < arr2[j]) {
- i=(i+1)%m;
- i1++;
- }
- else if (arr2[j] < arr1[i]) {
- j=(j+1)%n;
- j1++;
- }
- else
- {
- //System.out.print(arr2[j++]+" ");
- C[k]=arr2[j++%m];
- k++;
- i=(i+1)%m;
- i1++;
- j1++;
- }
- }
- int [] intersect=new int [k];
- for(int l=0;l<intersect.length;l++){
- intersect[l]=C[l];
- }
- return intersect;
- }
- static int[] Union(int arr1[], int arr2[],int st1,int st2)
- {
- int m=arr1.length;
- int n=arr2.length;
- int []C=new int[m+n];
- int k=0;
- int i1=0,j1=0;
- int i = st1, j = st2;
- while (i1 < m && j1 < n)
- {
- if (arr1[i] < arr2[j])
- {
- if(check(C,arr1[i],k)){
- C[k]=arr1[i];
- k++;
- i1++;
- }else{
- i1++;
- }
- i=(i+1)%m;
- }
- //System.out.print(arr1[i++]+" ");
- else if (arr2[j] < arr1[i]){
- if(check(C,arr2[j],k)){
- C[k]=arr2[j];
- k++;
- j1++;
- }else{
- j1++;
- }
- j=(j+1)%n;
- }
- //C[k++]=arr2[j++];
- //System.out.print(arr2[j++]+" ");
- else
- {
- if(check(C,arr2[j],k)){
- C[k++]=arr2[j++];
- i1++;
- }
- else{
- j1++;
- //System.out.print(arr2[j++]+" ");
- i1++;
- }
- i=(i+1)%m;
- j=(j+1)%n;
- }
- }
- /* Print remaining elements of
- the larger array */
- while(i1 < m){
- C[k++]=arr1[i++%m];
- i1++;
- }
- //System.out.print(arr1[i++]+" ");
- while(j1 < n) {
- C[k++]=arr2[j++%n];
- j1++;
- }
- //System.out.print(arr2[j++]+" ");
- //return 0;
- int [] newArr=new int[k];
- for(int l=0;l<newArr.length;l++){
- newArr[l]=C[l];
- }
- return newArr;
- }
- public static boolean check(int []A,int elem,int idx){
- boolean b_C=true;
- for(int i=0;i<idx;i++){
- if(A[i]==elem){
- b_C=false;
- break;
- }
- }
- return b_C;
- }
- public static Node nodeIntersection(Node h1, Node h2) {
- Node head = new Node(h1.element, null);
- Node tail = head;
- for (Node n = h1.next; n != null; n = n.next) {
- if (checkList(h2, n.element)) {
- if(!checkList(head,n.element)){
- Node mn = new Node(n.element, null);
- tail.next = mn;
- tail = mn;
- }
- }
- }
- for (Node n = h2; n != null; n = n.next) {
- if (checkList(h1, n.element)) {
- if(!checkList(head,n.element)){
- Node mn = new Node(n.element, null);
- tail.next = mn;
- tail = mn;
- }
- }
- }
- return head;
- }
- public static boolean checkList(Node h, Object elem) {
- for (Node n = h; n != null; n = n.next) {
- if (n.element.equals(elem)) {
- return true;
- }
- }
- return false;
- }
- public static Node nodeUnion(Node h1,Node h2){
- Node head=new Node(h1.element,null);
- Node tail=head;
- for(Node n=h1.next;n!=null;n=n.next){
- if(!checkList(head,n.element)){
- Node mn=new Node(n.element,null);
- tail.next=mn;
- tail=mn;
- }
- }
- for(Node n=h2;n!=null;n=n.next){
- if(!checkList(head,n.element)){
- Node mn=new Node(n.element,null);
- tail.next=mn;
- tail=mn;
- }
- }
- return head;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement