-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy path16_Set_Operation.c
114 lines (100 loc) · 1.84 KB
/
16_Set_Operation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
//Set Operations on Arrays
struct Array
{
int A[10];
int size;
int length;
};
void Display(struct Array arr)
{
int i;
printf("\nElements are\n");
for(i=0;i<arr.length;i++)
printf("%d ",arr.A[i]);
}
struct Array* Union(struct Array *arr1,struct Array *arr2)
{
int i,j,k;
i=j=k=0;
struct Array *arr3=(struct Array *)malloc(sizeof(struct
Array));
while(i<arr1->length && j<arr2->length)
{
if(arr1->A[i]<arr2->A[j])
arr3->A[k++]=arr1->A[i++];
else if(arr2->A[j]<arr1->A[i])
arr3->A[k++]=arr2->A[j++];
else
{
arr3->A[k++]=arr1->A[i++];
j++;
}
}
for(;i<arr1->length;i++)
arr3->A[k++]=arr1->A[i];
for(;j<arr2->length;j++)
arr3->A[k++]=arr2->A[j];
arr3->length=k;
arr3->size=10;
return arr3;
}
struct Array* Intersection(struct Array *arr1,struct Array
*arr2)
{
int i,j,k;
i=j=k=0;
struct Array *arr3=(struct Array *)malloc(sizeof(struct
Array));
while(i<arr1->length && j<arr2->length)
{
if(arr1->A[i]<arr2->A[j])
i++;
else if(arr2->A[j]<arr1->A[i])
j++;
else if(arr1->A[i]==arr2->A[j])
{
arr3->A[k++]=arr1->A[i++];
j++;
}
}
arr3->length=k;
arr3->size=10;
return arr3;
}
struct Array* Difference(struct Array *arr1,struct Array
*arr2)
{
int i,j,k;
i=j=k=0;
struct Array *arr3=(struct Array *)malloc(sizeof(struct
Array));
while(i<arr1->length && j<arr2->length)
{
if(arr1->A[i]<arr2->A[j])
arr3->A[k++]=arr1->A[i++];
else if(arr2->A[j]<arr1->A[i])
j++;
else
{
i++;
j++;
}
}
for(;i<arr1->length;i++)
arr3->A[k++]=arr1->A[i];
arr3->length=k;
arr3->size=10;
return arr3;
}
int main()
{
struct Array arr1={{2,9,21,28,35},10,5};
struct Array arr2={{2,3,9,18,28},10,5};
struct Array *arr3;
arr3=Union(&arr1,&arr2);
Display(*arr3);
return 0;
}