Răspuns :
#include <stdlib.h>
#define SIZE 100000
struct IntSet {
int n;
int *arr;
};
struct IntSet_Operations{
struct IntSet (*create_empty_set)();
void (*add_element)(struct IntSet set, int x);
struct IntSet (*_union)(struct IntSet set1, struct IntSet set2);
struct IntSet (*intersection)(struct IntSet set1, struct IntSet set2);
} ops;
struct IntSet create_empty_set_fun(){
struct IntSet set;
set.n = 0;
set.arr = malloc(SIZE * sizeof(int));
return set;
}
void add_element_fun(struct IntSet set, int x){
set.arr[set.n] = x;
set.n++;
}
int frecv_vec[200000000];
void zero_frecv(){
for(int i = 0; i < 200000000; i++)frecv_vec[i] = 0;
}
struct IntSet union_fun(struct IntSet set1, struct IntSet set2){
zero_frecv();
struct IntSet set = ops.create_empty_set();
for(int i = 0; i < set1.n; i++)
frecv_vec[set1.arr[i] + 100000000]++;
for(int i = 0; i < set2.n; i++)
frecv_vec[set2.arr[i] + 100000000]++;
for(int i = -100000000; i <= 100000000; i++)
if(frecv_vec[i+100000000])
ops.add_element(set, i);
return set;
}
struct IntSet intersection_fun(struct IntSet set1, struct IntSet set2){
zero_frecv();
struct IntSet set = ops.create_empty_set();
for(int i = 0; i < set1.n; i++)
frecv_vec[set1.arr[i] + 100000000]=1;
for(int i = 0; i < set2.n; i++)
if(frecv_vec[set2.arr[i]+100000000] == 1)
frecv_vec[set2.arr[i] + 100000000] = 2;
for(int i = -100000000; i <= 100000000; i++)
if(frecv_vec[i+100000000] == 2)
ops.add_element(set, i);
return set;
}
void init(){
ops.create_empty_set = &create_empty_set_fun;
ops.add_element = &add_element_fun;
ops._union=&union_fun;
ops.intersection = &intersection_fun;
}