有集合A和集合B,要求设计生成集合C=A∩B的算法,其中集合A,B和C都用链式存储结构表示。
(这个和合并两个有序的链表相似)
typedef struct node{
char data;
struct node *next;
}Node,Sqlist;
bool find(Sqlist *head, Node *p){
int tag =0;
Node *q = head->next;
while(q){
if(q->data == p->data)
tag = 1;
q = q->next;
}
if(tag == 1)
return true;
else return false;
}
//head3 是结果
void A_and_B(Sqlist *head1,Sqlist *head2, Sqlist *head3){
Node *p, *m;
p = head1->next; m = head3; m->next = NULL;
while(p){
if(find(head2,p) == true){
Node *n = (Node *)malloc(sizeof(Node));
n->data = p->data;
n->next = m->next;
m->next = n;
}
p = p->next;
}
}



