この形式の単一リンクリストでそれぞれ表される2つのセットの交差と差を取得しようとしています
struct node{
unsigned n;
struct node *next;
};
リスト内の要素の数を計算し、特定の要素がリストに含まれているかどうかを判断する前のタスクで、この関数を既に作成しました。
int cardinality(struct node *s){
struct node *tmp = s;
int count = 0;
while(tmp != NULL){
tmp = tmp->next;
count++;
}
return count;
}
int contains(struct node *s, int v){ /* returns 0 if in list, 1 if not in list */
struct node *tmp = s;
int contains = 1;
while(tmp->next != NULL){
if(tmp->n == v){
contains = 0;
break;
} else{
if(tmp == NULL) break;
tmp = tmp->next;
}
}
return contains;
}
次の関数をコーディングする必要がありますが、その方法がわかりません。1 つのリストをループし、リスト内のすべての要素について 2 番目のリストをループして、2 番目のリストに含まれているかどうか (違い) を確認する必要がありますか? このタスクは複雑に思えますが、もっと簡単な方法があるはずです。あなたが私を助けてくれることを願っています
void intersection(struct node *s1, struct node *s2, struct node **result){
}
void difference(struct node *s1, struct node *s2, struct node **result){
}