ecco ho modificato il codice così:
codice:
struct distinti{
int val;
int flag;
struct distinti *next;
};
typedef struct distinti distinti;
typedef struct distinti *testa;
int diffvals(int a[], int b[], int n){
testa m=NULL;
int f,count;
for(int i=0;i<n;i++){
if(verifica(m))
m=add_head(m,a[i]);
else{
while(m!=NULL){
if(a[i]!=m->val)
f=1;
else
f=0;
m=m->next;
}
if(f==1)
m=add_head(m,a[i]);
}
}
for(int i=0;i<n;i++){
while(m!=NULL){
if(b[i]==m->val)
m->flag=1; //b[i] è gia presente nella lista
else
m->flag=0; //b[i] non è presente nella lista
m=m->next;
}
}
for(int i=0;i<n;i++){
if(verifica(m))
m=add_head(m,b[i]);
else{
while(m!=NULL){
if(b[i]!=m->val)
f=1;
else
f=0;
m=m->next;
}
if(f==1)
m=add_head(m,b[i]);
}
}
while(m!=NULL){
if(m->flag!=1)
count++;
}
svuota(m);
return count;
}
testa add_head(testa m, int valore){
testa nuovo=malloc(sizeof(distinti));
nuovo->val=valore;
nuovo->next=m;
return nuovo;
}
void svuota(testa m){
testa temp;
if(m!=NULL){
do {
temp=m;
m=m->next;
free(temp);
}while(m!=NULL);
}
else
return;
}
int verifica(testa m){
int s=0;
if(m==NULL)
s=1;
return s;
} //ritorna 1 se è vuota, 0 se non lo è
però non riesco a capire perchè all'inizio quando la lista è vuota insierisce correttamente il primo valore del vettore A, poi il secondo sovrascrivo il primo nella lista e così via, quindi la lista è sempre e solo di un elemento